Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
int r=tr.dtsender.Rows.Count-1;
                            int i=dt.Rows.Count-1;
                            tr.txtsbefore.Text=dt.Rows[i]["Total_Amount_Debit"].ToString();
                            double total=Convert.ToDouble(dt.Rows[i]["Total_Amount_Debit"]);
                            double bal=Convert.ToDouble(txtamount.Text);
                            Double diff=total-bal;
                            tr.txtsafter.Text=diff.ToString();
                            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();
                            }
                            tr.dtsender.Rows[r].Cells["Total_Amount_Debit"].Value=diff.ToString();
Posted
Comments
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:
tr.dtsender.Rows[r].Cells["Total_Amount_Debit"].Value=diff.ToString();

Hello,

Don't do:
C#
tr.dtsender.Rows[r].Cells["Total_Amount_Debit"].Value=diff.ToString();


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:
C#
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:
C#
r=tr.dtsender.Rows.Count-1;
tr.dtsender.Rows[r].Cells["Total_Amount_Debit"].Value=diff.ToString();


Valery.
 
Share this answer
 
Comments
zeshanazam 16-Jan-13 17:31pm    
if{DataTable1.Amount_Debit}=0 then "NO Amount is credited yet"
else
sum{DataTable1.Amount_Debit}
i want to check if amount debit column is empty then it just show string msg else count sum of colum otherwise not.
try to check Row number is correct or not: means u check below underline code's row number like what is "r".
C#
tr.dtsender.<big>Rows[r]</big>.Cells["Total_Amount_Debit"].Value=diff.ToString();
 
Share this answer
 
Hi,

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.
 
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