Click here to Skip to main content
14,238,068 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Friends...

Here i want to get the data grid view last rows value

i tried like this,
txtid.Text =dataGridView1.Rows[dataGridView1.RowCount].Cells[348].Value.ToString;

it gives error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


what is the problem i am not getting,

please help me...

thanks in advance..
Posted
Updated 16-Jun-19 20:02pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try like this..



txtid.Text =dataGridView1.Rows[dataGridView1.Rows.Count -1 ].Cells[348].Value.ToString;
   
Comments
basurajkumbhar 5-Dec-13 8:03am
   
Thanks for reply..
But it gives error
like

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Karthik_Mahalingam 5-Dec-13 8:33am
   
hi check the dataGridView1.Columns.Count it should be greater than 348 else you will get index out of range error...
Karthik_Mahalingam 5-Dec-13 8:33am
   
do u have more than 348 columns ??
Rate this:
Please Sign up or sign in to vote.

Solution 2

You're using dataGridView1.RowCount which will return the total count of rows. Now, assuming that the value 348 is correct, always remember that while trying to fetch a value from a collection, like this, whose index are zero based, the last value can be accessed as follows:

txtid.Text =dataGridView1.Rows[(dataGridView1.RowCount  - 1)].Cells[348].Value.ToString;



For ex in a collection, ABC of length 10, if you write ABC[ABC.Length], it means you're tring to access the 11th index of the collection which is not there, hence this error.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100