網路大神的寫法,
紀錄並學習。
初始化時加入
public Form1()
{
InitializeComponent();
//加入全選
ckeckboxSelectAll .dgv = dataGridView1;
ckeckboxSelectAll .AddSelectAllcheckbox();
}
cb.Location 調整參數到指定位置
public class ckeckboxSelectAll
{
public static System.Windows.Forms.DataGridView dgv;
public static void AddSelectAllcheckbox()
{
System.Windows.Forms.CheckBox cb= new System.Windows.Forms.CheckBox();
cb.Text = "";
cb.Checked = false;
System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(0, -1, true);
cb.Size = new System.Drawing.Size(13, 13);
cb.Location = new Point(rect.Location.X + dgv.Columns[0].Width - 55, rect.Location.Y + 3);
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
dgv.Controls.Add(cb);
}
static void cb_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();
}
}
沒有留言:
張貼留言