Solved

Gridview total

Posted on 2009-05-18
12
911 Views
Last Modified: 2012-05-07
Hiii... I have One small asp.net aplication . in this gridview is bounded but column is dynamically created ie ) table which bound the gridview that is created at runtime so please help me to do total in footer...
0
Comment
Question by:mohitbhatia5
  • 6
  • 6
12 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24418976
If you are using DataSet or DataTable to bind your grid dynamically use one of the following versions of the code in the RowDataBound event of GridView assuming you have a lable named lblTotalRecords and lblTotalDisplayedRecords.
'Page Level variable defined

Private m_intTotalRecords As Integer
 

'During data binding store total number of records in a page-level variable

m_intTotalRecords = DataSet1.Tables(0).Rows.Count
 

'OR
 

m_intTotalRecords = DataTable1.Rows.Count
 
 

'Write the following in RowDataBound event of GridView
 

If e.Row.RowType = DataControlRowType.Footer Then
 

  'Number of total records

  CType(e.Row.Cells(0).FindControl("lblTotalRecords"), Label).Text = m_intTotalRecords.ToString()

  'Number of records displayed

  CType(e.Row.Cells(0).FindControl("lblTotalDisplayedRecords"), Label).Text = GridView1.Rows.Count
 

End If

Open in new window

0
 

Author Comment

by:mohitbhatia5
ID: 24419061
I m using asp.net with c#...
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24419083
See the code below:
//Page Level variable defined

private int m_intTotalRecords;
 

//During data binding store total number of records in a page-level variable

m_intTotalRecords = DataSet1.Tables(0).Rows.Count;
 

//OR
 

m_intTotalRecords = DataTable1.Rows.Count;

    

    

//Write the following in RowDataBound event of GridView
 

if (e.Row.RowType == DataControlRowType.Footer) {

  

  //Number of total records

  ((Label)e.Row.Cells(0).FindControl("lblTotalRecords")).Text = m_intTotalRecords.ToString();

  //Number of records displayed

  ((Label)e.Row.Cells(0).FindControl("lblTotalDisplayedRecords")).Text = GridView1.Rows.Count;

}

Open in new window

0
 

Author Comment

by:mohitbhatia5
ID: 24419446
this code tells the no. of rows in grid...but i want sum total of data of each column..
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 125 total points
ID: 24419556
If you atleast know the number of columns being created dynamically, you may acheive this in GridView_PreRender event. Using a loop traverse through the rows and for each coloumn note the value (to be summed) in a separate variable (one for each column).  Have a check in the loop to check if the RowType is DataRow and if the RowType is Footer then write out these values. Try using the following code in GridView_PreRender.

int intColumn1 = 0;

int intColumn2 = 0;
 

foreach (GridViewRow row in GridView1.Rows)

{

  if (row.RowType == DataControlRowType.DataRow)

  {

      intColumn1 += (int)row.Cells(0).Text;

      intColumn2 += (int)row.Cells(1).Text;

  }

  

  if (row.RowType == DataControlRowType.Footer)

  {

      ((Label)row.Cells(0).FindControl("lblTotalColumn1")).Text = intColumn1.ToString();

      ((Label)row.Cells(2).FindControl("lblTotalColumn2")).Text = intColumn2.ToString();

  }

}

Open in new window

0
 

Author Comment

by:mohitbhatia5
ID: 24420069
hiiiii... you r right nw but i have an error which i attached ....this error become when i comment the if loop..if i dont comment then total will not display.. i hv one more problem regard this...which is where i place the lables in footer ...because column of grid are autogenerated...

please help me....
error.bmp
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24420166
The attached image is totally black. Please save it as JPEG instead of BMP in paint or whatever tool you used to take snapshot and re-attach.
For your problem in footer, make sure you have set ShowFooter property of the GridView to True. Further, replace line 14 and 15 of the above code with the code below, however make sure you are passing right index value in Cells():

row.Cells(0).Text = intColumn1.ToString();

row.Cells(1).Text = intColumn2.ToString();

Open in new window

0
 

Author Comment

by:mohitbhatia5
ID: 24420353
Every thing is fine but when i comment if loop ....
when not comment then following error is their...showfooter prot is true...
error.JPG
0
 

Author Comment

by:mohitbhatia5
ID: 24420367
Show this
error.JPG
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24420467
Please use the code I posted in my last comment.
0
 

Author Comment

by:mohitbhatia5
ID: 24420516
yes i have use that code but that code will  display total in last row of the grid only when i comment the
if (row.RowType == DataControlRowType.Footer)
            {
}
loop...instead of footer of the grid....when i remove the comment from if loop the it will show the error...
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24420714
Please use the following as guided and for more details on a similar solution see the following link:
http://www.asp.net/Learn/data-access/tutorial-15-vb.aspx

//use these lines outside the loop and remove if statemetn for rowtype = footer

GridView1.FooterRow.Cells(0).Text = m_intColumn1.ToString();

GridView1.FooterRow.Cells(1).Text = m_intColumn2.ToString();
 

//another alternative would be this solution

//create these variable at page level

int m_intColumn1 = 0;

int m_intColumn2 = 0;
 

//use this code in RowDataBound event rather than PreRender event

if (e.Row.RowType == DataControlRowType.DataRow)

{

    intColumn1 += (int)e.Row.Cells(0).Text;

    intColumn2 += (int)e.Row.Cells(1).Text;

}
 

if (e.Row.RowType == DataControlRowType.Footer)

{

    e.Row.Cells(0).Text = m_intColumn1.ToString();

    e.Row.Cells(1).Text = m_intColumn2.ToString();

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now