【发布时间】:2015-09-03 08:19:07
【问题描述】:
我有一个读取到字符串列表的 csv 规范文件。然后将其添加到 datagridview 并添加一个复选框列。我想检查所有复选框,但没有任何效果。尝试循环遍历每一行并将值设置为 true,还尝试设置行事件所需的默认值,但没有运气。有什么想法吗?
DataTable dtSpecs = new DataTable();
string[] tmpHeaders = specList[0].Split(',');
foreach (var header in tmpHeaders)
{
dtSpecs.Columns.Add(header, typeof(string));
}
for (int i = 1; i < specList.Count; i++)
{
dtSpecs.Rows.Add(specList[i].Split(','));
}
dataGridView2.DataSource = dtSpecs;
DataGridViewCheckBoxColumn ckb2 = new DataGridViewCheckBoxColumn();
ckb2.HeaderText = "Enable";
ckb2.ValueType = typeof(bool);
dataGridView2.Columns.Insert(0, ckb2);
//This didn't work
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
dataGridView2.Rows[i].Cells[0].Value = true;
}
//This way also didn't work
foreach (DataGridViewRow row in dataGridView2.Rows)
{
row.Cells[0].Value = true;
}
//Also tried this but still nothing checked
private void dataGridView2_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[0].Value = true;
}
【问题讨论】:
-
这段代码,为我工作,请您提供更多关于您的加载页面或页面中其他方法的代码。
标签: c# checkbox datagridview