[英]Checking a checkbox in DataGridview whether it is checked or not?
我有Windows窗體中的DataGridView
,其中包含13個復選框。 一個checbox即(第一個)是檢查所有其他基本為幾個月的復選框。 因此,現在我希望當我選中第一個復選框時,所有其他復選框都應該選中,而當我取消選中第一個復選框時,所有復選框都應該選中。 當我選中第一個復選框時,我的代碼可以正常工作,但是當我取消選中第一個復選框時,仍選中所有復選框。 但我希望他們不受限制。 我已經使用了CellContentClick
事件。
這是我的代碼。
if (e.ColumnIndex == 1)
{
for (int k = 2; k <= 13; k++)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[k];
DataGridViewCheckBoxCell checkCell = cell as DataGridViewCheckBoxCell;
checkCell.Value = true;
}
}
值返回對象類型,並且不能與布爾值進行比較。 您可以將值轉換為布爾值
if ((bool)row.Cells[1].Value == true)
{
// what I want to do
}
嘗試這個 :
foreach (GridViewRow gvrow in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk != null & chk.Checked)
{
str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
strname += gvrow.Cells[2].Text + ',';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.