DataGridView的復(fù)選框列的選中事件:DataGirdCheckBoxColumn 列 選中 事件,。 今天在winform下使用DataGridView時(shí),,使用了checkbox列,。一般我們可以通過一個(gè)按鈕的單擊事件中,來判斷該列是否被選中,。但這樣似乎不太人性化,。因此改為當(dāng)checkbox被選中時(shí),執(zhí)行任務(wù),。
可別小看了這個(gè)問題,。我試了很多的datagridview事件,比如 cellClick CellContentChanged 等事件,,要不就是無法撲捉,,要不就是得到的值為選中前的值:false 。 當(dāng)然,,最后無意找到了正確的事件:cellContentClick 事件,。在該事件中可得知選中前后的值,代碼如下: private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ if (e.ColumnIndex == 3 && Convert.ToInt32(tbCpiName.Tag)>0 )//當(dāng)單擊復(fù)選框,,同時(shí)處于組合編輯狀態(tài)時(shí) { DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; bool ifcheck1 = Convert.ToBoolean(cell.FormattedValue); bool ifcheck2 = Convert.ToBoolean(cell.EditedFormattedValue); if (ifcheck1 != ifcheck2) { BCItem bcitem = new BCItem(); bcitem.B_bciID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["bciID"].Value); bcitem.C_CpiID = Convert.ToInt32(tbCpiName.Tag); if (ifcheck2)//將該項(xiàng)目與組合關(guān)聯(lián) { bllcp.AddCompageAndItem(bcitem);//關(guān)聯(lián) } else { bllcp.DeleteCompageAndItem(bcitem);//取消關(guān)聯(lián) } } } } |
|