Friday, May 6, 2011

How can I get a value from a CheckBoxColumn using C# 3.0?

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?

From stackoverflow
  • 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.

    Angel Escobedo : Stills showing null reference.
  • 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'
              });
            }
    

0 comments:

Post a Comment