在winform开发中,很多时候需要用到DataGridView这个控件,当需要对数据进行选择后进行其他操作的时候,就需要给控件加上一个CheckBox,使之能够全选、取消全选或选择某些记录,参考网上案例,写了一个方法如下,方便大家参考使用。
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace InpatientCaregiversIS.Common
{
public static class DataGridViewHelper
{
public static DataGridView AddCheckBoxToDgvAndBindDataSource(DataGridView dgv, DataTable dt)
{
int TotalCheckBoxes = 0;
int TotalCheckedCheckBoxes = 0;
CheckBox HeaderCheckBox = null;
bool IsHeaderCheckBoxClicked = false;
AddHeaderCheckBox();
HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
dgv.CellValueChanged += new DataGridVi


