我在datagridview中将列checkBox定位为column0。我想得到column0检查的column1的sum行为true。我用了这段代码:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
sum += double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
}
}
textBox1.Text = sum.ToString();
}
但它无法正常工作。
答案 0 :(得分:1)
这样的事情
textBox1.Text = (dataGridView1.Rows.Cast<DataGridViewRow>()
.Where( r => Convert.ToBoolean(r.Cells[0].Value).Equals(true))
.Sum(t => Convert.ToInt32(t.Cells[1].Value))).ToString();
答案 1 :(得分:1)
选择C#DataGridView中复选框列中的所有复选框:
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}