foreach (DataGridViewRow

R

Robert Bravery

HI All,

I'm trying to loop through a datagridview, and use values found in various
columns. But problem is that I get errors newar the end.
I'm assuming that it is reading the blank row at the bottom of the grid.
How can I code my foreach block so that I loop through all the datagridviews
rows less one, or less the last row

I'm using:
foreach (DataGridViewRow dr in dataGridView1.Rows)

Thanks
Robert
 
T

Tom Spink

Robert said:
HI All,

I'm trying to loop through a datagridview, and use values found in various
columns. But problem is that I get errors newar the end.
I'm assuming that it is reading the blank row at the bottom of the grid.
How can I code my foreach block so that I loop through all the
datagridviews rows less one, or less the last row

I'm using:
foreach (DataGridViewRow dr in dataGridView1.Rows)

Thanks
Robert

Hi Robert,

Try this:

///
foreach ( DataGridViewRow dr in dataGridView1.Rows )
if ( dr.IsNewRow )
continue;
///

I've used continue, as opposed to break, because it's nice to keep code
scalable, you never know when the blank row could move to the middle of the
Rows collection ;-). However, you could change it to 'break' if you really
want to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top