我有一个 Datagridview,其中有一个复选框列。我想检查特定单元格的复选框。绑定网格后,我在表单加载中使用了以下代码。
但它不起作用。
我正在使用 for 循环绑定网格。
DataGridViewCheckBoxColumn dgvc = new DataGridViewCheckBoxColumn();
dgvc.Name = "colCheck";
dgvc.Width = 50;
dgvc.DefaultCellStyle.BackColor = Color.Lavender;
dgvOption.Columns.Add(dgvc);
WebBrowserColumn wbc = new WebBrowserColumn();
wbc.Name = "colOptionText";
//wbc.Width = 500;
wbc.ReadOnly = true;
wbc.DefaultCellStyle.BackColor = Color.Lavender;
dgvOption.Columns.Add(wbc);
dgvOption.Columns.Add("colCorrect", "Correct");
dgvOption.Columns["colCorrect"].Visible = false;
dgvOption.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgvOption.AllowUserToResizeRows = false;
dgvOption.AllowUserToResizeColumns = false;
dgvOption.CellBorderStyle = DataGridViewCellBorderStyle.None;
dgvOption.ClearSelection();
int i = 0;
foreach (DataRow dtRow in dtOption.Rows)
{
dgvOption.Rows.Add();
dgvOption.Rows[i].Cells["colOptionText"].Value = dtRow["Options"].ToString();
dgvOption.Rows[i].Cells["colCorrect"].Value = dtRow["Correct"].ToString();
i += 1;
}
if(Answer !="")
{
dgvOption.Rows[2].Cells["colCheck"].Value = true;
}
private void dgvOption_SelectionChanged(object sender, EventArgs e)
{
dgvOption.ClearSelection();
}
private void dgvOption_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
foreach (DataGridViewRow dgvRow in dgvOption.Rows)
{
dgvRow.Cells["colCheck"].Value = false;
}
dgvOption.CurrentCell.Value = true;
}
}