// chk.ReadOnly = true;
chk.Width = 50;
chk.HeaderText = "状态";
dG_List.Columns.Add(chk);
DataGridView添加checkBox列
private void dG_List_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dG_List.Columns[e.ColumnIndex].HeaderText == "状态")
{
if (dG_List.Rows[e.RowIndex].Cells[6].ValueType == typeof(bool))
{
bool IsChk = (bool)dG_List.Rows[e.RowIndex].Cells[6].FormattedValue;
if (IsChk)
{
IsChk = false;
dG_List.Rows[e.RowIndex].Cells[6].Value = false;
}
else
{
IsChk = true;
dG_List.Rows[e.RowIndex].Cells[6].Value = true;
}
}
else
{
if (dG_List.Rows[e.RowIndex].Cells[6].ValueType == typeof(CheckState))
{
CheckState chk = (CheckState)dG_List.Rows[e.RowIndex].Cells[6].EditedFormattedValue;
if (chk == CheckState.Checked)
{
chk = CheckState.Unchecked;
dG_List.Rows[e.RowIndex].Cells[6].Value = false;
}
else
{
chk = CheckState.Checked;
dG_List.Rows[e.RowIndex].Cells[6].Value = true;
}
}
}
}
}
private void dG_List_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dG_List.IsCurrentCellDirty)
{
// Commit
dG_List.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dG_List_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dG_List.Rows[e.RowIndex].Cells[6].ValueType == typeof(bool))
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dG_List.Rows[e.RowIndex].Cells[6] as DataGridViewCheckBoxCell;
chk.ThreeState = true;
}
}