前几天一个朋友和我讨论,如何实现在行标题添加CheckBox列,实现全选功能,想了想这个应该是很常见的功能,,不过没有作过,找不到合适的属性来设置,试着重写一个BoolColumn组件,但是没有成功,在设置Header上面的CheckBox位置的时候总是没有合适的途径来作。
实在没有办法了,只好偷了个懒,在DataGrid上面加入一个CheckBox,可以基本实现功能,不过外观不是很好看,可以根据需要再进行调整。而且在设置CheckBox位置的时候遇到了重写组件相同的问题,无法确定Header 的位置。只有在填充数据之后再实现这个添加列标题的功能,没有数据,HeaderText仍然显示初始的设定,而不是CheckBox。
基本的实现如下:(权作抛砖引玉,希望各位朋友看到了有好的方法多多指点,在此谢过...)
1.设置CheckBox作为BoolColumn列标题的代码.
1// CheckBox which is defined as boolColumn header
2 private CheckBox chkSelectAll = null;
3 /// <summary>
4 /// Add a checkBox into dataGrid as dataColumn's header,for checking all rows.
5 /// </summary>
6 private void AddHeaderForSelectAll()
7 {
8 if (dtTemp == null || dtTemp.Rows.Count <= 0)
9 {
10 return ;
11 }
12 // if checkBox object exists, return
13 if (chkSelectAll != null)
14 {
15 return ;
16 }
17 chkSelectAll = new CheckBox();
18 chkSelectAll.Location = new Point(dataGrid1.GetCellBounds(0, 0).X , dataGrid1.GetCellBounds(0, 0).Y - dataGrid1.PreferredRowHeight);
19 chkSelectAll.Size = new Size(dcSelected.Width - 4 , dataGrid1.PreferredRowHeight - 2);
20 chkSelectAll.Text = "Select all";
21 chkSelectAll.Visible = true;
22 chkSelectAll.BackColor = SystemColors.Control;
23 chkSelectAll.CheckedChanged += new EventHandler(chkSelectAll_CheckedChanged);
24 this.dataGrid1.Controls.Add(chkSelectAll);
25 }
***这里需要把dcSelected修改为全局变量,不然在AddHeaderForSelectAll方法里面是无法访问的.2 private CheckBox chkSelectAll = null;
3 /// <summary>
4 /// Add a checkBox into dataGrid as dataColumn's header,for checking all rows.
5 /// </summary>
6 private void AddHeaderForSelectAll()
7 {
8 if (dtTemp == null || dtTemp.Rows.Count <= 0)
9 {
10 return ;
11 }
12 // if checkBox object exists, return
13 if (chkSelectAll != null)
14 {
15 return ;
16 }
17 chkSelectAll = new CheckBox();
18 chkSelectAll.Location = new Point(dataGrid1.GetCellBounds(0, 0).X , dataGrid1.GetCellBounds(0, 0).Y - dataGrid1.PreferredRowHeight);
19 chkSelectAll.Size = new Size(dcSelected.Width - 4 , dataGrid1.PreferredRowHeight - 2);
20 chkSelectAll.Text = "Select all";
21 chkSelectAll.Visible = true;
22 chkSelectAll.BackColor = SystemColors.Control;
23 chkSelectAll.CheckedChanged += new EventHandler(chkSelectAll_CheckedChanged);
24 this.dataGrid1.Controls.Add(chkSelectAll);
25 }
2.为CheckBox列标题添加事件,以实现全选行..
1/// <summary>
2 ///
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
7 {
8 if (chkSelectAll.Checked)
9 {
10 SelectAllRows(true);
11 }
12 else
13 {
14 SelectAllRows(false);
15 }
16 }
17
18 /// <summary>
19 /// Check all rows or not
20 /// </summary>
21 /// <param name="bSelect"></param>
22 private void SelectAllRows(bool bSelect)
23 {
24 if (this.dtTemp != null && this.dtTemp.Rows.Count > 0)
25 {
26 foreach(DataRow dr in dtTemp.Rows)
27 {
28 dr["IsSelected"] = bSelect;
29 }
30 }
31 dtTemp.AcceptChanges();
32 }
2 ///
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
7 {
8 if (chkSelectAll.Checked)
9 {
10 SelectAllRows(true);
11 }
12 else
13 {
14 SelectAllRows(false);
15 }
16 }
17
18 /// <summary>
19 /// Check all rows or not
20 /// </summary>
21 /// <param name="bSelect"></param>
22 private void SelectAllRows(bool bSelect)
23 {
24 if (this.dtTemp != null && this.dtTemp.Rows.Count > 0)
25 {
26 foreach(DataRow dr in dtTemp.Rows)
27 {
28 dr["IsSelected"] = bSelect;
29 }
30 }
31 dtTemp.AcceptChanges();
32 }
调用的时候只要在填充数据之后执行此方法即可:
3.调用例子
1 this.dataGrid1.DataSource = dtTemp.DefaultView;
2 AddHeaderForSelectAll();
2 AddHeaderForSelectAll();
这种实现很牵强,总感觉很不妥,希望各位看到的朋友,不吝笔墨留言指点赐教,多谢。