views:

140

answers:

2

I can get the current selected row in this way:

 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];
}

So, Now I want to get all of the rows that have been checked by the user:

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

}

I am getting null reference from the debugger.

What am I doing wrong?

+1  A: 

I suspect you're going about it wrong, and what you actually meant to write is this:

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

}

But I'm not certain.

mquander
Stills showing null reference.
Angel Escobedo
A: 

This is the answer, Hopes helps (Thanks mquander for the .Any idea):

        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'
          });
        }
Angel Escobedo