In my gridview, there have a few column that contain number. At the bottom of the gridview, I want to add one row to display the total of the number for that certain column.

How to do that? i have no idea. can someone help me??

Hi Maria_mj

1) U should set ShowFooter="True" property of the gridview
2) Convert your intended Column to TemplateField by rt. clicking on grid , ShowSmartTag, Edit Column, "Convert this field into TemplateField" linkbutton

3)your code should look similar in .Aspx

<asp:TemplateField HeaderText="Total">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Total") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                             <asp:TextBox ID="txtGrandTotal" runat="server" Width="69px"></asp:TextBox>
                        </FooterTemplate>
                        
                    </asp:TemplateField>

4) something similar in aspx.vb

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbtn As LinkButton
            Dim txtPrice As Label

            lbtn = CType(e.Row.Cells(0).Controls(0), LinkButton)
            Dim drw As InvoiceDS.InvoiceRow
            drw = CType(CType(e.Row.DataItem, System.Data.DataRowView).Row, InvoiceDS.InvoiceRow)
            'lbtn.OnClientClick = String.Format("return confirm('Are you sure to delete the {0} Product');", drw.ProductName.ToString)
            Utility.ShowDeleteConfirmBox(lbtn, drw.ProductName.ToString)

            txtPrice = CType(e.Row.FindControl("Label1"), Label)
            If Not IsNothing(txtPrice) Then
                If txtPrice.Text <> "" Then

                    dblPrice += txtPrice.Text

                End If
                Session("dblPrice") = dblPrice
            End If

        End If
        If e.Row.RowType = DataControlRowType.Footer Then
            Dim txtTot As TextBox
            txtTot = CType(e.Row.FindControl("txtGrandTotal"), TextBox)
            If Not IsNothing(txtTot) Then
                txtTot.Text = dblPrice
            End If
        End If
    End Sub

Mark as solved if it helps you!!!

You need to set ShowFooter = True in inline code(in aspx file).
now in the Codebehind, you need to handle RawDataBound Event of GridView. in the Event, write condition for each DataRow and perform calculation for whichever the columns you want.. take a look in the below code :

If e.Row.RowType = DataControlRowType.DataRow Then
tot += Integer.Parse(DataBinder.Eval(e.Row.DataItem, "unitsinstock").ToString)
End If
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(2).Text = tot.ToString
End If

here, change the value of Cells(2) to your cell where you want to show total.

Hope this will help you..

===========================================================================================

In my gridview, there have a few column that contain number. At the bottom of the gridview, I want to add one row to display the total of the number for that certain column.

How to do that? i have no idea. can someone help me??

I am writing code in C#.
I think, the code that you posted is in VB.
Can you give me the sample code in C#?
One more thing, how to handle RawDataBound Event of GridView?
Do i have to double click the GridView?
When i double click the GridView,it will generate the GridView1_SelectedIndexChanged.
Or i just have to write all of the code manually?

sure :-) and sorry for late response. the below is C# code.

if (e.Row.RowType == DataControlRowType.DataRow) 
{
    tot += int.Parse(DataBinder.Eval(e.Row.DataItem, "unitsinstock").ToString);
}
if (e.Row.RowType == DataControlRowType.Footer) 
{
    e.Row.Cells(2).Text = tot.ToString;
}

you can use this web site to convert your code either from C# to VB or from VB to C#..
http://www.developerfusion.com/tools/convert/csharp-to-vb/

regarding RowDataBound event, don't double click on your Gridview. Just Select your GridView, go the Properties -> click on Event symbol -> you will see RawDataBound event there, just double click there. and copy-paste above code in event.

hope this will help you..

I am writing code in C#.
I think, the code that you posted is in VB.
Can you give me the sample code in C#?
One more thing, how to handle RawDataBound Event of GridView?
Do i have to double click the GridView?
When i double click the GridView,it will generate the GridView1_SelectedIndexChanged.
Or i just have to write all of the code manually?

dear rohand;

i have try to do as your suggestion. but, when i debug the code, i got 3 errors:

1) The best overloaded method match for 'int.Parse(string)' has some invalid arguments
2) Argument '1':cannot convert from 'method group' to 'string'
3) Non-invocable member "System.Web.UI.WebControls.TableRow.Cells'cannot be used like a method

Here is my code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace BDAS
{
    public partial class ReportPipelineHot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int tot;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                tot += int.Parse(DataBinder.Eval(e.Row.DataItem, "project_revenue").ToString);
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells(3).Text = tot.ToString;
            }
        }
    }
}

Oh sorry, your code will looks like below :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int tot;

if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
tot += int.Parse(rowView["project_revenue"].ToString());
}

if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells(3).Text = tot.ToString();
}
}

hope this will help :-)
========================================================================================

dear rohand;

i have try to do as your suggestion. but, when i debug the code, i got 3 errors:

1) The best overloaded method match for 'int.Parse(string)' has some invalid arguments
2) Argument '1':cannot convert from 'method group' to 'string'
3) Non-invocable member "System.Web.UI.WebControls.TableRow.Cells'cannot be used like a method

Here is my code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace BDAS
{
    public partial class ReportPipelineHot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int tot;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                tot += int.Parse(DataBinder.Eval(e.Row.DataItem, "project_revenue").ToString);
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells(3).Text = tot.ToString;
            }
        }
    }
}

i have make the changes to my code.
but, still got an error:
Non-invocable member 'System.Web.UI.WebControls.TableRow.Cells' cannot be used like a method.

The error occur at -> e.Row.Cells(3).Text = tot.ToString();

This is my code:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int tot;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowView = (DataRowView)e.Row.DataItem;
                tot += int.Parse(rowView["txtsubTotal"].ToString());
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells(3).Text = tot.ToString();
            }
        }

one more thing... i am not sure about the
tot += int.Parse(rowView["txtsubTotal"].ToString());
what should i put in the -> rowView[" "] ?
is it the name of textbox that i have to create in the gridview footer?
or is it the name of column in the database table?

rowView ["database column name"] not the textbox control name.

Also I guess on the third cell position in footer, you have put txtsubTotal your textbox in aspx side under footer template. So that first you need to search control like :

TextBox txtTotal = e.Row.FindControl("txtsubTotal");
txtTotal.Text = tot.ToString();

OR

TextBox txtTotal = e.Row.Cells(3).FindControl("txtsubTotal");
txtTotal.Text = tot.ToString();

try by both ways i have written. But i believe first one is correct, If it throws error then try by second way.


hope it will fix error.

========================================================================================

i have make the changes to my code.
but, still got an error:
Non-invocable member 'System.Web.UI.WebControls.TableRow.Cells' cannot be used like a method.

The error occur at -> e.Row.Cells(3).Text = tot.ToString();

This is my code:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int tot;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowView = (DataRowView)e.Row.DataItem;
                tot += int.Parse(rowView["txtsubTotal"].ToString());
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells(3).Text = tot.ToString();
            }
        }

one more thing... i am not sure about the
tot += int.Parse(rowView["txtsubTotal"].ToString());
what should i put in the -> rowView[" "] ?
is it the name of textbox that i have to create in the gridview footer?
or is it the name of column in the database table?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.