Click here to Skip to main content
13,295,953 members (53,370 online)
Rate this:
Please Sign up or sign in to vote.
See more:
int r=tr.dtsender.Rows.Count-1;
                            int i=dt.Rows.Count-1;
                            double total=Convert.ToDouble(dt.Rows[i]["Total_Amount_Debit"]);
                            double bal=Convert.ToDouble(txtamount.Text);
                            Double diff=total-bal;
                            for (int j = 0; j <= dt.Rows.Count - 1; j++)
                                int newRowIndex = tr.dtsender.Rows.Add();
                                tr.dtsender.Rows[newRowIndex].Cells["Account_No"].Value = dt.Rows[j]["Account_No"].ToString();
                                tr.dtsender.Rows[newRowIndex].Cells["Transaction_no"].Value = dt.Rows[j]["Transaction_no"].ToString();
                                tr.dtsender.Rows[newRowIndex].Cells["Amount_Debit"].Value = dt.Rows[j]["Amount_Debit"].ToString();
                                tr.dtsender.Rows[newRowIndex].Cells["Debit_Date"].Value = dt.Rows[j]["Debit_Date"].ToString();
                                tr.dtsender.Rows[newRowIndex].Cells["Total_Amount_Debit"].Value = dt.Rows[j]["Total_Amount_Debit"].ToString();
Posted 16-Jan-13 4:23am
Sandeep Mewara 16-Jan-13 9:52am
Error is clear that the index is more than number of rows. But, I don't see it currently in the code you share. What do you see when you debugged it?
zeshanazam 16-Jan-13 9:57am
index was out of range. when i comment this line,data is loading in grid view but i want to change value of last row of cell Total_Amount_Debit
Sandeep Mewara 16-Jan-13 10:03am
When you debug, what is the index?
zeshanazam 16-Jan-13 9:59am
error is "Index was out of range. Must be non-negative and less than the size of the collection."
Sandeep Mewara 16-Jan-13 10:03am
You say error in this line:
zeshanazam 16-Jan-13 10:11am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Don't do:

Basically you initialise at the beginning of the code your variable r to the number of rows -1, then later in your loop you are adding news rows:
int newRowIndex = tr.dtsender.Rows.Add();

So if at the beginning you have 0 rows r will be negative, and as you do not recalculate it the exception "Index was out of range. Must be non-negative and less than the size of the collection." will be throws as r is -1.

So... recalculated r before using it as the number of rows as changed:

zeshanazam 16-Jan-13 17:31pm
if{DataTable1.Amount_Debit}=0 then "NO Amount is credited yet"
i want to check if amount debit column is empty then it just show string msg else count sum of colum otherwise not.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try to check Row number is correct or not: means u check below underline code's row number like what is "r".
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


If your grid is set to allow user to add there is always an extra row. Check for that and either do not allow edit or use row count -2.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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