mohitbhatia5
asked on
Gridview total
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...
ASKER
I m using asp.net with c#...
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;
}
ASKER
this code tells the no. of rows in grid...but i want sum total of data of each column..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
please help me....
error.bmp
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():
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();
ASKER
Every thing is fine but when i comment if loop ....
when not comment then following error is their...showfooter prot is true...
error.JPG
when not comment then following error is their...showfooter prot is true...
error.JPG
ASKER
Show this
error.JPG
error.JPG
Please use the code I posted in my last comment.
ASKER
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...
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...
Please use the following as guided and for more details on a similar solution see the following link:
http://www.asp.net/Learn/d ata-access /tutorial- 15-vb.aspx
http://www.asp.net/Learn/d
//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