[英]How do I restrict user to select only single checkbox in gridview which i created dynamically
我已經在winform應用程序中動態地在數據網格視圖中創建了一個復選框。
DataGridViewCheckBoxColumn myCheckedColumn = new DataGridViewCheckBoxColumn()
{
Name = "MyColumn",
FalseValue = 0,
TrueValue = 1,
Visible = true
};
我想限制用戶一次只選擇一個復選框。
是否有直接財產?
我用谷歌搜索並找到了這個答案,但是CellContentClick事件沒有觸發,因為復選框是動態創建的。
private void mydatagridview_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
{
int currentcolumnclicked = e.ColumnIndex;
int currentrowclicked = e.RowIndex;
foreach (DataGridViewRow dr in associatinggridView.Rows)
{
dr.Cells[currentcolumnclicked].Value = false;
}
associatinggridView.CurrentRow.Cells[currentrowclicked].Value = true;
}
}
}
在我的測試中,無論如何創建其列,所有單元格的CellContentClick都會升高。
void mydatagridview_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int col = yourCheckboxColumnIndex;
for (int r = 0; r < mydatagridview.Rows.Count; r++)
if (r != e.RowIndex && e.ColumnIndex == col )
mydatagridview[col , r].Value = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.