Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Try like this..



C#
txtid.Text =dataGridView1.Rows[dataGridView1.Rows.Count -1 ].Cells[348].Value.ToString;
 
Share this answer
 
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    
hicheck 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 ??
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900