DataGridView为表头增加批量选择框
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataGridView为表头增加批量选择框相关的知识,希望对你有一定的参考价值。
/// <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为表头增加批量选择框的主要内容,如果未能解决你的问题,请参考以下文章