Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping either through DataGridViewRowCollection or DataGridViewSelectedRowCollection

I want to loop through DataGridViewRowCollection or DataGridViewSelectedRowCollection (users choice). I don't know how I can do it the simple way. Here is my Code:

List<DataGridViewRow> rows = new List<DataGridViewRow>();

if (dr == DialogResult.No)
    foreach (DataGridViewRow row in dgvResult.Rows)
        rows.Add(row);
else if (dr == DialogResult.Yes)
    foreach (DataGridViewRow row in dgvResult.SelectedRows)
        rows.Add(row);

int counter = 1;

foreach (DataGridViewRow row in rows)
{
    //...
}
like image 358
it-west.net Avatar asked Nov 24 '12 11:11

it-west.net


1 Answers

You may need Enumerable.Cast method.

  List<DataGridViewRow> lst = dataGridView1.Rows.Cast<DataGridViewRow>().ToList();
like image 120
Adil Avatar answered Oct 28 '22 09:10

Adil