Click here to Skip to main content
Rate this: bad
good
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 3:23am
Comments
Sandeep Mewara at 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 at 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 at 16-Jan-13 10:03am
   
When you debug, what is the index?
zeshanazam at 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 at 16-Jan-13 10:03am
   
You say error in this line:
tr.dtsender.Rows[r].Cells["Total_Amount_Debit"].Value=diff.ToString();
zeshanazam at 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 at 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>.Cells["Total_Amount_Debit"].Value=diff.ToString();
  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
0 CPallini 405
1 Sergey Alexandrovich Kryukov 262
2 OriginalGriff 165
3 George Jonsson 134
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
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