码迷,mamicode.com
首页 > Windows程序 > 详细

DataGridView为表头增加批量选择框

时间:2016-01-23 12:46:57      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>  
        /// 给DataGridView添加全选  
        /// </summary>  
        public class AddCheckBoxToDataGridView  
        {  
            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 = true;  
                System.Drawing.Rectangle rect =  
                    dgv.GetCellDisplayRectangle(0, -1, true);  
                ckBox.Size = new System.Drawing.Size(dgv.Columns[0].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();  
            }  
        }

 

调用方法

AddCheckBoxToDataGridView.dgv = dgvKS_RY;  
                    AddCheckBoxToDataGridView.AddFullSelect();

DataGridView为表头增加批量选择框

标签:

原文地址:http://www.cnblogs.com/ilooking/p/5152934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!