winform 实现全选

来源:互联网 发布:新秀网络兼职平台官网 编辑:程序博客网 时间:2024/05/21 13:02
 
//添加类
public    class AddckboxAll    {        public static System.Windows.Forms.DataGridView dgv;        public static void AddFullSelect()        {            if (dgv.Rows.Count < 1)            {                return;            }            System.Windows.Forms.CheckBox ckBox = new System.Windows.Forms.CheckBox();            ckBox.Text = "全选";            ckBox.Checked = false;            System.Drawing.Rectangle rect =                dgv.GetCellDisplayRectangle(0, -1, true);            ckBox.Size = new System.Drawing.Size(dgv.Columns[1].Width, 18);            ckBox.Location = rect.Location;            ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);            dgv.Controls.Add(ckBox);        }        static void ckBox_CheckedChanged(object sender, EventArgs e)        {            for (int i = 0; i < dgv.Rows.Count; i++)            {                dgv.Rows[i].Cells[0].Value = ((System.Windows.Forms.CheckBox)sender).Checked;            }            dgv.EndEdit();        }    }}


//在用到的窗体加上

AddckboxAll.dgv = dataGridView1;

 AddckboxAll.AddFullSelect();

注: 窗体里的代码要写在 绑定数据后面。

原创粉丝点击