/// <summary>
/// 设置DataGridView多一个CheckBox的列
/// </summary>
/// <param name="dgv">要添加Checkbox列的DataGridView控件</param>
public static void setCheckBoxHead(DataGridView dgv)
{
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
CCFS.Framework.Util.DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
colCB.HeaderCell = cbHeader;
colCB.Width = 24;//.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;//.AllCells;
dgv.Columns.Insert(0, colCB);
cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
}
/// <summary>
/// 往指定的DataGridView控件中添加可以设置为Readonly的Checkbox列
/// </summary>
/// <param name="dgv">操作的DataGridView控件</param>
/// <param name="hasReadOnly">标识是否需要添加只读的Checkbox列</param>
public static void setCheckBoxHead(DataGridView dgv,bool hasReadOnly)
{
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
CCFS.Framework.Util.DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
colCB.HeaderCell = cbHeader;
colCB.Width = 24;//.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;//.AllCells;
dgv.Columns.Insert(0, colCB);
if (hasReadOnly)
{
cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnReadOnlyCheckBoxClicked);
}
else
{
cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
}
}
/// <summary>
///
/// </summary>
/// <param name="dgv"></param>
/// <param name="state"></param>
private static void cbHeader_OnReadOnlyCheckBoxClicked(DataGridView dgv, bool state)
{
int chkCount = dgv.Rows.Count;
//dgv.ClearSelection();
if (state)
{
for (int i = 0; i < chkCount; i++)
{
bool cbRow = (bool)dgv.Rows[i].Cells[0].EditedFormattedValue;
if (!cbRow)
{
if (!dgv.Rows[i].Cells[0].ReadOnly)
{
dgv.Rows[i].Cells[0].Value = true;
}
}
}
if (chkCount > 0)
{
int iRowNum = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible && i > 0)
{
iRowNum = i;
break;
}
}
dgv.CurrentCell = dgv[iRowNum, 0];
dgv.SelectAll();
}
}
else
{
for (int i = 0; i < chkCount; i++)
{
bool cbRow = (bool)dgv.Rows[i].Cells[0].EditedFormattedValue;
if (cbRow)
{
if (!dgv.Rows[i].Cells[0].ReadOnly)
{
dgv.Rows[i].Cells[0].Value = false;
}
}
}
if (chkCount > 0)
{
int iRowNum = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible && i > 0)
{
iRowNum = i;
break;
}
}
dgv.CurrentCell = dgv[iRowNum, 0];
dgv.ClearSelection();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="dgv"></param>
/// <param name="state"></param>
private static void cbHeader_OnCheckBoxClicked(DataGridView dgv, bool state)
{
int chkCount = dgv.Rows.Count;
//dgv.ClearSelection();
if (state)
{
for (int i = 0; i < chkCount; i++)
{
bool cbRow = (bool)dgv.Rows[i].Cells[0].EditedFormattedValue;
if (!cbRow)
{
dgv.Rows[i].Cells[0].Value = true;
}
}
if (chkCount > 0)
{
int iRowNum = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible && i > 0)
{
iRowNum = i;
break;
}
}
dgv.CurrentCell = dgv[iRowNum, 0];
dgv.SelectAll();
}
}
else
{
for (int i = 0; i < chkCount; i++)
{
bool cbRow = (bool)dgv.Rows[i].Cells[0].EditedFormattedValue;
if (cbRow)
{
dgv.Rows[i].Cells[0].Value = false;
}
}
if (chkCount > 0)
{
int iRowNum = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible && i > 0)
{
iRowNum = i;
break;
}
}
dgv.CurrentCell = dgv[iRowNum, 0];
dgv.ClearSelection();
}
}
}