DataGridView是个好控件,虽然微软对其封装的很好,但真正运用到实际项目还是有点游刃而不有余。
由于实际需要只好对其进行不完全的二次封装,毕竟工期很紧,没有闲情逸致搞完整封装。
代码
1 ///coffeeliu.cnblogs.com
2 ///code by coffee.liu
3 ///2010.3.28
4 ///
5 //将其整合进datagridview的column工厂,这点有点像流行的XPTable处理方式。
2 ///code by coffee.liu
3 ///2010.3.28
4 ///
5 //将其整合进datagridview的column工厂,这点有点像流行的XPTable处理方式。
//通过重绘基本可以向DataGridView添加进任何控件,前提是你要能在内存中绘出所需控件,实际上微软的控件也是这么绘制出来的。
6 public class DataGridViewExCheckboxColumn : DataGridViewCheckBoxColumn
7 {
8 public DataGridViewExCheckboxColumn()
9 {
10 this.CellTemplate = new DataGridViewExCheckBoxCell();
11 }
12 }
13 //真正的单元格类
14 public class DataGridViewExCheckBoxCell : DataGridViewCheckBoxCell
15 {
16 private bool showoriginal;//是否显示原始的DataGridViewCheckBoxCell
17 private string _text;//checkbox.text属性
18 CheckBoxState state = CheckBoxState.UncheckedNormal;
19 public string Text
20 {
21 get { return _text; }
22 set { _text = value; }
23 }
24 public object Value
25 {
26 get { return base.Value; }
27 set { base.Value = value; }
28 }
29 public bool ShowOriginal
30 {
31 get
32 {
33 return showoriginal;
34 }
35 set
36 {
37 showoriginal = value;
38 }
39 }
40 public override object Clone()
41 {
42 DataGridViewExCheckBoxCell cell =
43 (DataGridViewExCheckBoxCell)base.Clone();
44 cell.Value = base.Value;
45 cell.ShowOriginal = this.ShowOriginal;
46 cell.Text = this.Text;
47 return cell;
48 }
49 public DataGridViewExCheckBoxCell()
50 {
51
52 this.ShowOriginal = false;
53
54 this.Text = "";
55
56 }
57 protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
58 {
59 base.OnMouseDown(e);
60 if (!Convert.ToBoolean(this.Value))
61 {
62 this.Value = true;
63 state = CheckBoxState.CheckedNormal;
64 DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex);
65 this.RaiseCellValueChanged(ee);
66
67 }
68 else
69 {
70 this.Value = false;
71 state = CheckBoxState.UncheckedNormal;
72 DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex);
73 this.RaiseCellValueChanged(ee);
74 }
75 }
76 protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
77 {
78 base.OnMouseUp(e);
79
80 }
81 protected override void Paint(Graphics graphics,
82 Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
83 DataGridViewElementStates elementState, object value,
84 object formattedValue, string errorText,
85 DataGridViewCellStyle cellStyle,
86 DataGridViewAdvancedBorderStyle advancedBorderStyle,
87 DataGridViewPaintParts paintParts)
88 {
89 base.Paint(graphics, clipBounds, cellBounds, rowIndex,
90 elementState, value, formattedValue, errorText,
91 cellStyle, advancedBorderStyle, paintParts);
92 Rectangle textRectangleValue = new Rectangle();
93 if (!this.ShowOriginal)
94 {
95 // 绘出底色
96 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
97 {
98 SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
99 graphics.FillRectangle(cellBackground, cellBounds);
100 cellBackground.Dispose();
101 }
102
103 // 绘出边框
104 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
105 {
106 PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
107 }
108 Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
109 Point center = new Point(cellBounds.X, cellBounds.Y);
110 center.X += 3; //(cellBounds.Width - size.Width) / 2;
111 center.Y += (cellBounds.Height - size.Height) / 2;
112 textRectangleValue.X =
113 CheckBoxRenderer.GetGlyphSize(graphics,
114 CheckBoxState.UncheckedNormal).Width + cellBounds.X+2;
115 textRectangleValue.Y = cellBounds.Y;
116 textRectangleValue.Width = cellBounds.Width -
117 CheckBoxRenderer.GetGlyphSize(graphics,
118 CheckBoxState.UncheckedNormal).Width;
119 textRectangleValue.Height = cellBounds.Height
120 -(cellBounds.Height-CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width)/2-1;
121 if (Convert.ToBoolean(this.Value))
122 state = CheckBoxState.CheckedNormal;
123 else
124 state = CheckBoxState.UncheckedNormal;
125 CheckBoxRenderer.DrawCheckBox(graphics, center, textRectangleValue, _text, SystemFonts.DefaultFont, TextFormatFlags.Bottom, false, state);
126 }
127 else
128 { //考虑实际需要有可能需要其显示微软的默认checkbox样式
129 base.Paint(graphics, clipBounds, cellBounds, rowIndex,
130 elementState, value, formattedValue, errorText,
131 cellStyle, advancedBorderStyle, paintParts);
132
133 }
134 }
135 }
6 public class DataGridViewExCheckboxColumn : DataGridViewCheckBoxColumn
7 {
8 public DataGridViewExCheckboxColumn()
9 {
10 this.CellTemplate = new DataGridViewExCheckBoxCell();
11 }
12 }
13 //真正的单元格类
14 public class DataGridViewExCheckBoxCell : DataGridViewCheckBoxCell
15 {
16 private bool showoriginal;//是否显示原始的DataGridViewCheckBoxCell
17 private string _text;//checkbox.text属性
18 CheckBoxState state = CheckBoxState.UncheckedNormal;
19 public string Text
20 {
21 get { return _text; }
22 set { _text = value; }
23 }
24 public object Value
25 {
26 get { return base.Value; }
27 set { base.Value = value; }
28 }
29 public bool ShowOriginal
30 {
31 get
32 {
33 return showoriginal;
34 }
35 set
36 {
37 showoriginal = value;
38 }
39 }
40 public override object Clone()
41 {
42 DataGridViewExCheckBoxCell cell =
43 (DataGridViewExCheckBoxCell)base.Clone();
44 cell.Value = base.Value;
45 cell.ShowOriginal = this.ShowOriginal;
46 cell.Text = this.Text;
47 return cell;
48 }
49 public DataGridViewExCheckBoxCell()
50 {
51
52 this.ShowOriginal = false;
53
54 this.Text = "";
55
56 }
57 protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
58 {
59 base.OnMouseDown(e);
60 if (!Convert.ToBoolean(this.Value))
61 {
62 this.Value = true;
63 state = CheckBoxState.CheckedNormal;
64 DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex);
65 this.RaiseCellValueChanged(ee);
66
67 }
68 else
69 {
70 this.Value = false;
71 state = CheckBoxState.UncheckedNormal;
72 DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex);
73 this.RaiseCellValueChanged(ee);
74 }
75 }
76 protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
77 {
78 base.OnMouseUp(e);
79
80 }
81 protected override void Paint(Graphics graphics,
82 Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
83 DataGridViewElementStates elementState, object value,
84 object formattedValue, string errorText,
85 DataGridViewCellStyle cellStyle,
86 DataGridViewAdvancedBorderStyle advancedBorderStyle,
87 DataGridViewPaintParts paintParts)
88 {
89 base.Paint(graphics, clipBounds, cellBounds, rowIndex,
90 elementState, value, formattedValue, errorText,
91 cellStyle, advancedBorderStyle, paintParts);
92 Rectangle textRectangleValue = new Rectangle();
93 if (!this.ShowOriginal)
94 {
95 // 绘出底色
96 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
97 {
98 SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
99 graphics.FillRectangle(cellBackground, cellBounds);
100 cellBackground.Dispose();
101 }
102
103 // 绘出边框
104 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
105 {
106 PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
107 }
108 Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
109 Point center = new Point(cellBounds.X, cellBounds.Y);
110 center.X += 3; //(cellBounds.Width - size.Width) / 2;
111 center.Y += (cellBounds.Height - size.Height) / 2;
112 textRectangleValue.X =
113 CheckBoxRenderer.GetGlyphSize(graphics,
114 CheckBoxState.UncheckedNormal).Width + cellBounds.X+2;
115 textRectangleValue.Y = cellBounds.Y;
116 textRectangleValue.Width = cellBounds.Width -
117 CheckBoxRenderer.GetGlyphSize(graphics,
118 CheckBoxState.UncheckedNormal).Width;
119 textRectangleValue.Height = cellBounds.Height
120 -(cellBounds.Height-CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width)/2-1;
121 if (Convert.ToBoolean(this.Value))
122 state = CheckBoxState.CheckedNormal;
123 else
124 state = CheckBoxState.UncheckedNormal;
125 CheckBoxRenderer.DrawCheckBox(graphics, center, textRectangleValue, _text, SystemFonts.DefaultFont, TextFormatFlags.Bottom, false, state);
126 }
127 else
128 { //考虑实际需要有可能需要其显示微软的默认checkbox样式
129 base.Paint(graphics, clipBounds, cellBounds, rowIndex,
130 elementState, value, formattedValue, errorText,
131 cellStyle, advancedBorderStyle, paintParts);
132
133 }
134 }
135 }
做好之后即可以像正常使用datagridview一样调用DataGridViewExCheckboxColumn了
调用方法:
代码
DataGridViewExCheckboxColumn checkboxColumn;
checkboxColumn = CreateCheckBoxColumn();
dataGridView1.Columns.Add(checkboxColumn);
....
dataGridView1.Rows.Add(new object[] { true,false});
((DataGridViewExCheckBoxCell)dataGridView1.Rows[0].Cells[0]).Text = "正向有功总电量";
((DataGridViewExCheckBoxCell)dataGridView1.Rows[0].Cells[2]).Text = "反向有功总电量";
checkboxColumn = CreateCheckBoxColumn();
dataGridView1.Columns.Add(checkboxColumn);
....
dataGridView1.Rows.Add(new object[] { true,false});
((DataGridViewExCheckBoxCell)dataGridView1.Rows[0].Cells[0]).Text = "正向有功总电量";
((DataGridViewExCheckBoxCell)dataGridView1.Rows[0].Cells[2]).Text = "反向有功总电量";
当然还有一些关键事件的联动处理,涉及到保密性这里省略。
运行效果: