Click here to Skip to main content
12,622,568 members (33,137 online)
Rate this:
 
Please Sign up or sign in to 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 16-Jan-13 4:23am
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();
zeshanazam 16-Jan-13 10:11am
   
yup.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

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

Valery.
  Permalink  
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.
Rate this: bad
 
good
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".
tr.dtsender.<big>Rows[r]</big>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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

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 | Mobile
Web01 | 2.8.161128.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2016
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