DataGridView的 DataGridViewCheckBoxColumn 沒有專門的「勾選事件」,判斷哪些行被勾選需遍歷行集合檢查單元格值。
List<DataGridViewRow> checkedRows = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows) {
// 忽略新行(允許用戶添加時的最後一行)
if (row.IsNewRow) continue;
// 假設勾選框在第0列,或用列名 "chkCol"
var cellValue = row.Cells["chkCol"].Value; // 或 row.Cells[0].Value
if (cellValue != null && (bool)cellValue == true) {
checkedRows.Add(row);
}
}
// 處理選中行
foreach (var row in checkedRows) {
// 讀取該行其他列數據
string id = row.Cells["IdCol"].Value.ToString();
// ... 業務邏輯
}
bool(綁定布爾字段時)或 bool?(可為空),轉型前需非空判斷。CellContentClick 事件,調用 EndEdit() 提交編輯,再讀取值。