在 Windows Forms 應用程式中,`DataGridView` 加入 `DataGridViewCheckBoxColumn` 後,點擊核取方塊**無法切換狀態** (不打勾、不取消),或需點擊兩次才生效。
`DataGridView` 預設行為:
使用者預期「單擊即切換」,但預設需要「雙擊」。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 1. 確認點擊的是 CheckBox 欄 (索引 0) 且非標題列
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
// 2. 關鍵:強制結束編輯模式,將值提交給資料來源
// 這會觸發 CellValueChanged 事件
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
// 3. (可選) 立即讀取更新後的值進行業務邏輯
bool isChecked = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
// 執行對應業務邏輯...
}
}
結合 `CurrentCellDirtyStateChanged` 自動提交:
// 表單建構子或 Load 事件綁定
dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_CurrentCellDirtyStateChanged;
dataGridView1.CellValueChanged += DataGridView1_CellValueChanged;
// 1. 檢測到 CheckBox 狀態改變 (髒狀態) 時,立即提交
private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
// 只對 CheckBox 欄位處理
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}
// 2. 值真正寫入資料來源後觸發,執行業務邏輯
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0) // CheckBox 欄
{
bool isChecked = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
string id = dataGridView1.Rows[e.RowIndex].Cells["IdColumn"].Value?.ToString();
// 業務邏輯:更新資料庫、統計選取數量、啟用/停用按鈕...
UpdateSelectionStatus(id, isChecked);
}
}
// 1. 在 DataGridView 初始化時加入標題列 CheckBox
private void SetupHeaderCheckBox()
{
var headerCheckBox = new DataGridViewCheckBoxColumn
{
Name = "SelectAll",
HeaderText = "全選",
Width = 50,
FalseValue = false,
TrueValue = true,
ThreeState = false
};
dataGridView1.Columns.Insert(0, headerCheckBox);
// 訂閱標題列點擊事件 (CellContentClick 不觸發標題列)
dataGridView1.CellClick += DataGridView1_CellClick;
}
// 2. 處理標題列點擊
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex == -1) // 標題列
{
bool selectAll = !(bool)dataGridView1.Rows[0].Cells[0].Value; // 簡單切換邏輯
// 更精確:讀取標題列 CheckBox 狀態需自行繪製或使用 HeaderCell
// 遍歷所有資料列設定值
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[0].Value = selectAll;
}
dataGridView1.RefreshEdit(); // 刷新編輯狀態
}
}
// 3. 更專業:自訂 HeaderCell 繪製 CheckBox (可參考 DataGridViewCheckBoxHeaderCell 開源實作)
| 資料來源 | Value 屬性型別 | TrueValue/FalseValue 設定 |
|---|---|---|
| DataTable | Boolean / DBNull | 自動對應 bool |
| List<T> (實體類別) | bool / bool? | 屬性型別為 bool 即可 |
| BindingList<T> | bool | 自動雙向同步 |
// 實體類別範例
public class OrderItem
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; } // 直接繫結到 CheckBoxColumn
}
// 繫結
var list = new BindingList(GetData());
dataGridView1.DataSource = list;
// CheckBoxColumn 設定
var chkCol = new DataGridViewCheckBoxColumn
{
DataPropertyName = "IsSelected", // 關鍵:對應實體屬性
HeaderText = "選取",
Width = 50
};
dataGridView1.Columns.Insert(0, chkCol);
DataGridView CheckBox 「點不動」的核心解決之道:
此模式符合 WinForms 編輯架構設計,兼顧使用者體驗 (單點即切換) 與資料一致性 (明確提交)。