001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Drawing;
004.
using
System.Linq;
005.
using
System.Text;
006.
using
System.Windows.Forms;
007.
008.
namespace
TORServices.Forms.Datagridview
009.
{
010.
#region _DataGridViewDialog
011.
/// <summary>
012.
/// Hosts a collection of DataGridViewTextBoxCell cells.
013.
/// </summary>
014.
public
class
DataGridViewDialogColumn : System.Windows.Forms.DataGridViewColumn
015.
{
016.
private
bool
showBrowseButton;
017.
[System.ComponentModel.Browsable(
true
)]
018.
019.
[System.ComponentModel.DefaultValue(
false
)]
020.
[System.ComponentModel.Category(
"Appearance"
)]
021.
[System.ComponentModel.Description(
"Show a button in each cell for browsing for files."
)]
022.
public
enum
SelectedPathType { colorDialog, folderDialog, fontDialog, OpenFileDialog, SaveFileDialog }
023.
public
bool
ShowBrowseButton
024.
{
025.
get
{
return
showBrowseButton; }
026.
set
027.
{
028.
029.
showBrowseButton = value;
030.
}
031.
}
032.
033.
protected
internal
event
EventHandler ButtonColumnClick;
034.
public
virtual
void
browseColumnButton_Click(
object
sender, EventArgs e)
035.
{
036.
if
(ButtonColumnClick !=
null
)
037.
{
038.
ButtonColumnClick(
this
, e);
039.
}
040.
041.
}
042.
043.
public
override
object
Clone()
044.
{
045.
var clm =
base
.Clone()
as
DataGridViewDialogColumn;
046.
DataGridViewDialogColumn xxx =
base
.Clone()
as
DataGridViewDialogColumn;
047.
if
(clm !=
null
)
048.
{
049.
clm.SelectedMode = _selected;
050.
clm.ShowBrowseButton = showBrowseButton;
051.
}
052.
return
clm;
053.
054.
}
055.
private
bool
useOpenFileDialogOnButtonClick;
056.
[System.ComponentModel.Browsable(
true
)]
057.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
058.
[System.ComponentModel.DefaultValue(
false
)]
059.
[System.ComponentModel.Category(
"Behavior"
)]
060.
[System.ComponentModel.Description(
"Dialog is dispalyed and on success the contents of the Cell is replaced with the new file path."
)]
061.
public
bool
UseOpenFileDialogOnButtonClick
062.
{
063.
064.
get
{
return
useOpenFileDialogOnButtonClick; }
065.
set
066.
{
067.
useOpenFileDialogOnButtonClick = value;
068.
}
069.
}
070.
private
SelectedPathType _selected;
071.
[System.ComponentModel.Browsable(
true
)]
072.
073.
074.
[System.ComponentModel.Category(
"Behavior"
)]
075.
[System.ComponentModel.Description(
"Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path."
)]
076.
public
SelectedPathType SelectedMode
077.
{
078.
get
{
return
_selected; }
079.
set
080.
{
081.
_selected = value;
082.
}
083.
}
084.
public
DataGridViewDialogColumn()
085.
:
base
(
new
DataGridViewDialogCell())
086.
{
087.
088.
}
089.
090.
public
override
System.Windows.Forms.DataGridViewCell CellTemplate
091.
{
092.
get
093.
{
094.
return
base
.CellTemplate;
095.
}
096.
set
097.
{
098.
if
(
null
!= value &&
099.
!value.GetType().IsAssignableFrom(
typeof
(DataGridViewDialogCell)))
100.
{
101.
throw
new
InvalidCastException(
"must be a DataGridViewDialogCell"
);
102.
}
103.
base
.CellTemplate = value;
104.
}
105.
}
106.
}
107.
108.
/// <summary>
109.
/// Displays editable text information in a DataGridView control. Uses
110.
/// PathEllipsis formatting if the column is smaller than the width of a
111.
/// displayed filesystem path.
112.
/// </summary>
113.
114.
public
class
DataGridViewDialogCell : DataGridViewLinkCell
115.
{
116.
public
Button browseButton;
117.
118.
Dictionary<Color, SolidBrush> brushes =
new
Dictionary<Color, SolidBrush>();
119.
120.
protected
virtual
SolidBrush GetCachedBrush(Color color)
121.
{
122.
if
(
this
.brushes.ContainsKey(color))
123.
return
this
.brushes[color];
124.
SolidBrush brush =
new
SolidBrush(color);
125.
this
.brushes.Add(color, brush);
126.
return
brush;
127.
}
128.
129.
protected
virtual
bool
RightToLeftInternal
130.
{
131.
get
132.
{
133.
return
this
.DataGridView.RightToLeft == RightToLeft.Yes;
134.
}
135.
}
136.
137.
protected
override
void
OnClick(DataGridViewCellEventArgs e)
138.
{
139.
base
.OnClick(e);
140.
if
(
this
.DataGridView.CurrentCell ==
this
)
141.
{
142.
string
_Value = Convert.ToString(
base
.Value);
143.
DataGridViewDialogColumn filePathColumn = (DataGridViewDialogColumn)
this
.DataGridView.Columns[ColumnIndex];
144.
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.OpenFileDialog)
145.
{
146.
if
(!System.IO.File.Exists(_Value))
147.
{ MessageBox.Show(
"ไม่พบ "
+ _Value); }
148.
else
149.
{ System.Diagnostics.Process.Start(_Value); }
150.
}
151.
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.folderDialog)
152.
{
153.
if
(!System.IO.Directory.Exists(_Value))
154.
{ MessageBox.Show(
"ไม่พบ "
+ _Value); }
155.
else
156.
{ System.Diagnostics.Process.Start(_Value); }
157.
}
158.
}
159.
}
160.
protected
override
void
Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
int
rowIndex, DataGridViewElementStates cellState,
object
value,
object
formattedValue,
string
errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
161.
{
162.
if
(cellStyle ==
null
) {
throw
new
ArgumentNullException(
"cellStyle"
); }
163.
this
.PaintPrivate(graphics, clipBounds, cellBounds, rowIndex, cellState, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
164.
}
165.
166.
protected
Rectangle PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
int
rowIndex, DataGridViewElementStates cellState,
object
formattedValue,
string
errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
167.
{
168.
System.Diagnostics.Debug.WriteLine(
string
.Format(
"Painting Cell row {0} for rowindex {2} with rectangle {1}"
,
this
.RowIndex, cellBounds, rowIndex));
169.
SolidBrush cachedBrush;
170.
Rectangle empty = Rectangle.Empty;
171.
if
(((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None)) {
this
.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); }
172.
Rectangle rectangle2 =
this
.BorderWidths(advancedBorderStyle);
173.
Rectangle borderedCellRectangle = cellBounds;
174.
borderedCellRectangle.Offset(rectangle2.X, rectangle2.Y);
175.
borderedCellRectangle.Width -= rectangle2.Right;
176.
borderedCellRectangle.Height -= rectangle2.Bottom;
177.
Point currentCellAddress =
base
.DataGridView.CurrentCellAddress;
178.
bool
isFirstCell = (currentCellAddress.X ==
base
.ColumnIndex) && (currentCellAddress.Y == rowIndex);
179.
bool
flagisFirstCellAndNotEditing = isFirstCell && (
base
.DataGridView.EditingControl !=
null
);
180.
bool
thisCellIsSelected = (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
181.
cachedBrush = ((((paintParts & DataGridViewPaintParts.SelectionBackground) != DataGridViewPaintParts.None) && thisCellIsSelected) && !flagisFirstCellAndNotEditing) ? GetCachedBrush(cellStyle.SelectionBackColor) : GetCachedBrush(cellStyle.BackColor);
182.
183.
if
(((((paintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None)) && ((cachedBrush.Color.A == 0xff) && (borderedCellRectangle.Width > 0))) && (borderedCellRectangle.Height > 0))
184.
{
185.
graphics.FillRectangle(cachedBrush, borderedCellRectangle);
186.
}
187.
if
(cellStyle.Padding != Padding.Empty)
188.
{
189.
if
(RightToLeftInternal)
190.
{
191.
borderedCellRectangle.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
192.
}
193.
else
194.
{
195.
borderedCellRectangle.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
196.
}
197.
borderedCellRectangle.Width -= cellStyle.Padding.Horizontal;
198.
borderedCellRectangle.Height -= cellStyle.Padding.Vertical;
199.
}
200.
if
(((isFirstCell) && (!flagisFirstCellAndNotEditing && ((paintParts & DataGridViewPaintParts.Focus) != DataGridViewPaintParts.None))) && ((ShowFocusCues &&
base
.DataGridView.Focused) && ((borderedCellRectangle.Width > 0) && (borderedCellRectangle.Height > 0))))
201.
{
202.
ControlPaint.DrawFocusRectangle(graphics, borderedCellRectangle, Color.Empty, cachedBrush.Color);
203.
}
204.
Rectangle cellValueBounds = borderedCellRectangle;
205.
string
text = formattedValue
as
string
;
206.
if
((text !=
null
) && (!flagisFirstCellAndNotEditing))
207.
{
208.
int
y = (cellStyle.WrapMode == DataGridViewTriState.True) ? 1 : 2;
209.
borderedCellRectangle.Offset(0, y);
210.
borderedCellRectangle.Width = borderedCellRectangle.Width;
211.
borderedCellRectangle.Height -= y + 1;
212.
if
((borderedCellRectangle.Width > 0) && (borderedCellRectangle.Height > 0))
213.
{
214.
TextFormatFlags flags = TextFormatFlags.PathEllipsis;
215.
216.
if
(((paintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None))
217.
{
218.
if
((flags & TextFormatFlags.SingleLine) != TextFormatFlags.GlyphOverhangPadding) { flags |= TextFormatFlags.EndEllipsis; }
219.
DataGridViewDialogColumn filePathColumn = (DataGridViewDialogColumn)
this
.DataGridView.Columns[ColumnIndex];
220.
221.
if
(
this
.RowIndex >= 0)
222.
{
223.
224.
bool
changed =
false
;
225.
if
((browseButton.Width != System.Math.Max(10, borderedCellRectangle.Width / 4)) && (browseButton.Width != 20))
226.
{
227.
System.Diagnostics.Trace.WriteLine(
string
.Format(
"browseButton Width was incorrect:{0} for given rectangle:{1}"
, browseButton.Width, borderedCellRectangle));
228.
browseButton.Width = System.Math.Max(10, borderedCellRectangle.Width / 4);
229.
browseButton.Width = System.Math.Min(browseButton.Width, 20);
230.
changed =
true
;
231.
}
232.
if
(browseButton.Height != (borderedCellRectangle.Height + 4))
233.
{
234.
System.Diagnostics.Trace.WriteLine(
string
.Format(
"browseButton Height was incorrect:{0} for given rectangle:{1}"
, browseButton.Height, borderedCellRectangle));
235.
browseButton.Height = borderedCellRectangle.Height + 4;
236.
changed =
true
;
237.
}
238.
Point loc =
new
Point();
239.
loc.X = borderedCellRectangle.X + borderedCellRectangle.Width - browseButton.Width;
240.
loc.Y = borderedCellRectangle.Y - 4;
241.
if
(browseButton.Location != loc)
242.
{
243.
System.Diagnostics.Trace.WriteLine(
string
.Format(
"browseButton location was incorrect:{0} for given rectangle:{1} with loc: {2}"
, browseButton.Location, borderedCellRectangle, loc));
244.
browseButton.Location = loc;
245.
changed =
true
;
246.
}
247.
if
(changed)
248.
browseButton.Invalidate();
249.
if
(!
this
.DataGridView.Controls.Contains(browseButton))
250.
this
.DataGridView.Controls.Add(browseButton);
251.
borderedCellRectangle.Width -= browseButton.Width;
252.
}
253.
}
254.
TextRenderer.DrawText(graphics, text, cellStyle.Font, borderedCellRectangle, thisCellIsSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor, flags);
255.
256.
257.
}
258.
}
259.
if
((
base
.DataGridView.ShowCellErrors) && ((paintParts & DataGridViewPaintParts.ErrorIcon) != DataGridViewPaintParts.None))
260.
{
261.
if
((!
string
.IsNullOrEmpty(errorText) && (cellValueBounds.Width >= 20)) && (cellValueBounds.Height >= 0x13))
262.
{
263.
Rectangle iconBounds =
this
.GetErrorIconBounds(graphics, cellStyle, rowIndex);
264.
if
((iconBounds.Width >= 4) && (iconBounds.Height >= 11))
265.
{
266.
iconBounds.X += cellBounds.X;
267.
iconBounds.Y += cellBounds.Y;
268.
Bitmap errorBitmap =
new
Bitmap(
typeof
(DataGridViewCell),
"DataGridViewRow.error.bmp"
);
269.
errorBitmap.MakeTransparent();
270.
if
(errorBitmap !=
null
)
271.
{
272.
lock
(errorBitmap)
273.
{
274.
graphics.DrawImage(errorBitmap, iconBounds, 0, 0, 12, 11, GraphicsUnit.Pixel);
275.
}
276.
}
277.
}
278.
}
279.
280.
}
281.
return
empty;
282.
}
283.
284.
public
bool
ShowFocusCues
285.
{
286.
get
{
return
true
; }
287.
}
288.
289.
protected
bool
ApplyVisualStylesToHeaders
290.
{
291.
get
292.
{
293.
if
(Application.RenderWithVisualStyles)
294.
{
295.
return
this
.DataGridView.EnableHeadersVisualStyles;
296.
}
297.
return
false
;
298.
}
299.
}
300.
301.
302.
303.
304.
305.
306.
307.
308.
309.
310.
void
browseButton_Click(
object
sender, EventArgs e)
311.
{
312.
DataGridViewDialogColumn filePathColumn = (DataGridViewDialogColumn)
this
.DataGridView.Columns[ColumnIndex];
313.
try
314.
{
315.
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.OpenFileDialog)
316.
{
317.
OpenFileDialog dialog =
new
OpenFileDialog();
318.
if
(dialog.ShowDialog() == DialogResult.OK)
319.
{
320.
321.
base
.Value = dialog.FileName.ToString();
322.
323.
}
324.
}
325.
else
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.folderDialog)
326.
{
327.
FolderBrowserDialog dialog =
new
FolderBrowserDialog();
328.
if
(dialog.ShowDialog() == DialogResult.OK)
329.
{
330.
base
.Value = dialog.SelectedPath;
331.
}
332.
}
333.
else
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.colorDialog)
334.
{
335.
ColorDialog dialog =
new
ColorDialog();
336.
if
(dialog.ShowDialog() == DialogResult.OK)
337.
{
338.
base
.Value = dialog.Color.ToString() +
":"
339.
+ dialog.Color.Name.ToString()
340.
+
": A="
+ dialog.Color.A +
" R="
+ dialog.Color.R +
" G="
+ dialog.Color.G +
" B="
+ dialog.Color.B;
341.
}
342.
}
343.
else
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.fontDialog)
344.
{
345.
FontDialog dialog =
new
FontDialog();
346.
if
(dialog.ShowDialog() == DialogResult.OK)
347.
{
348.
base
.Value = dialog.Font.ToString();
349.
}
350.
}
351.
else
if
(filePathColumn.SelectedMode == DataGridViewDialogColumn.SelectedPathType.SaveFileDialog)
352.
{
353.
SaveFileDialog dialog =
new
SaveFileDialog();
354.
if
(dialog.ShowDialog() == DialogResult.OK)
355.
{
356.
base
.Value = dialog.FileName;
357.
}
358.
}
359.
}
360.
catch
(Exception)
361.
{
362.
}
363.
filePathColumn.browseColumnButton_Click(
this
, e);
364.
365.
366.
367.
368.
369.
370.
}
371.
public
DataGridViewDialogCell()
372.
:
base
()
373.
{
374.
browseButton =
new
Button();
375.
browseButton.Text =
"…"
;
376.
browseButton.Click +=
new
EventHandler(browseButton_Click);
377.
}
378.
}
379.
#endregion
380.
}