Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting datagridview last row index

i'm trying to get the last row but the problem is....see my code

Int32 index=dataGridveiw1.Rows.Count; // this is count start 1,2,3,4,5,6

sum3=txt_lotweight.Text-txt_balanceweight.Text;
sum4=datagridview1.Rows[index].Cells["rollweight"].Value-sum3;

how to minus gridview last row value to sum3 in this code error will come rows index not found because rows count start from 1 and when i subtract rows value to sum3 its start from 0

so how to get last row of gridview

like image 839
user2491383 Avatar asked Jun 27 '13 07:06

user2491383


People also ask

How do I get the index of a selected row?

You can get the selected row indexes by using the selectedRowsIndexes property in Grid.


2 Answers

You are not getting last row index, but count that is higher by 1 than last index! That is because array indexing in C# starts from 0.

Int32 index = dataGridveiw1.Rows.Count - 1; // this is count start 1,2,3,4,5,6

this code will work. But I have doubts about your sum3 - if your TextBox contains integers you should cast it to int before subtracting, and Value in sum4 is object so casting is required as well.

like image 56
gzaxx Avatar answered Sep 21 '22 13:09

gzaxx


Index are basically starts from 0 so if you are using the row count then you have to use it like this to get the last index.

Int32 index=dataGridveiw1.Rows.Count - 1 ;
like image 34
vendettamit Avatar answered Sep 22 '22 13:09

vendettamit