如何使用C#3.0从CheckBoxColumn获取值?

时间:2009-05-07 19:33:24

标签: winforms datagridview lambda

我可以通过这种方式获取当前选定的行:

 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){

//Cells[0] cause CheckBoxColumn is in that index (first column)
DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells[0];
}

所以,现在我想获得用户检查过的所有行:

 foreach (var row_ in DataGridView1.Rows.OfType<DataGridViewRow>().
                                        Select(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                                         Where(r => r.Value.Equals(true))).FirstOrDefault()){

}

我从调试器获得null reference

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我怀疑你说错了,你真正打算写的是:

foreach (var row_ in
    DataGridView1.Rows.OfType<DataGridViewRow>().
    Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
    Any(r => r.Value.Equals(true))))
{

}

但我不确定。

答案 1 :(得分:0)

这就是答案,希望有所帮助(感谢mquander的。任意想法):

        foreach (var _row in dgvpendientepago.Rows.OfType<DataGridViewRow>().
            Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                Any(r => r.EditedFormattedValue.Equals(true))))
        {
          // do stuff like the following : 
          lst4Pay.Add(new Cobranzaciaseguro
          {
            numeroatencion = Convert.ToInt16(_row.Cells[3].Value),
            estado = 'P'
          });
        }