给dataGridView的标题处添加复选框,并实现全选功能


public delegate void CheckBoxClickedHandler( bool state);
         public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
         {
             bool _bChecked;
             public DataGridViewCheckBoxHeaderCellEventArgs( bool bChecked)
             {
                 _bChecked = bChecked;
             }
             public bool Checked
             {
                 get { return _bChecked; }
             }
         }
 
 
         public class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
         {
             Point checkBoxLocation;
             Size checkBoxSize;
             bool _checked = false ;
             Point _cellLocation = new Point();
             System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                 System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
             public event CheckBoxClickedHandler OnCheckBoxClicked;
 
             public DatagridViewCheckBoxHeaderCell()
             {
             }
 
             protected override void Paint(System.Drawing.Graphics graphics,
                 System.Drawing.Rectangle clipBounds,
                 System.Drawing.Rectangle cellBounds,
                 int rowIndex,
                 DataGridViewElementStates dataGridViewElementState,
                 object value,
                 object formattedValue,
                 string errorText,
                 DataGridViewCellStyle cellStyle,
                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
                 DataGridViewPaintParts paintParts)
             {
                 base .Paint(graphics, clipBounds, cellBounds, rowIndex,
                     dataGridViewElementState, value,
                     formattedValue, errorText, cellStyle,
                     advancedBorderStyle, paintParts);
                 Point p = new Point();
                 Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                 System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                 p.X = cellBounds.Location.X +
                     (cellBounds.Width / 2) - (s.Width / 2);
                 p.Y = cellBounds.Location.Y +
                     (cellBounds.Height / 2) - (s.Height / 2);
                 _cellLocation = cellBounds.Location;
                 checkBoxLocation = p;
                 checkBoxSize = s;
                 if (_checked)
                     _cbState = System.Windows.Forms.VisualStyles.
                         CheckBoxState.CheckedNormal;
                 else
                     _cbState = System.Windows.Forms.VisualStyles.
                         CheckBoxState.UncheckedNormal;
                 CheckBoxRenderer.DrawCheckBox
                 (graphics, checkBoxLocation, _cbState);
             }
 
             protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
             {
                 Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                 if (p.X >= checkBoxLocation.X && p.X <=
                     checkBoxLocation.X + checkBoxSize.Width
                 && p.Y >= checkBoxLocation.Y && p.Y <=
                     checkBoxLocation.Y + checkBoxSize.Height)
                 {
                     _checked = !_checked;
                     if (OnCheckBoxClicked != null )
                     {
                         OnCheckBoxClicked(_checked);
                         this .DataGridView.InvalidateCell( this );
                     }
 
                 }
                 base .OnMouseClick(e);
             }
         
     }

 

  然后在前台做个窗体,加个datagridview控件,加个几行,注意datagirdview有编辑状态,如果只有一行又是在编辑状态,就不起作用了。当然一般全选控件不可能在编辑状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load( object sender, EventArgs e)
        {
            //DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            //colCB.HeaderCell = cbHeader;
            //dataGridView1.Columns.Add(colCB);
            cbHeader.Value = string .Empty;
            cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
 
            dataGridView1.Columns[0].HeaderCell = cbHeader;
        }
 
        private void cbHeader_OnCheckBoxClicked( bool state)
        {        
              //这一句很重要结束编辑状态
        dgvWarn.EndEdit();
?
1
2
3
4
5
6
7
8
9
         if (dataGridView1.Rows.Count > 0)
         {
             for ( int i = 0; i < dataGridView1.Rows.Count; i++)
             {
                 dataGridView1.Rows[i].Cells[0].Value = state;
             }
         }
     }
}

  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值