adding totals to grid

Discussion in 'ASP .Net Datagrid Control' started by Carlos, Jun 9, 2004.

  1. Carlos

    Carlos Guest

    I need to add the total of each column at the end of the grdi How can I do
    that, here is my actual code to retreive the data.



    Dim SelectString As String
    '//Convert date from string to Date
    Dim L_Year As String
    Dim L_Site As String
    Dim L_Month As String
    L_Site = Str(Val(Request.QueryString("TSiteNumber")) + 1)
    L_Month = Request.QueryString("TMonths")
    L_Year = Request.QueryString("TYear")

    SelectString = "SELECT TSteID, TMine, TDate, TTime, TGrade, TSignal FROM
    Grade WHERE (YEAR(Tdate)=" + L_Year + ")" + " AND (MONTH(TDATE)=" + L_Month
    + ")" + " AND TSteID=" + L_Site + " ORDER BY TMine"

    '***************

    SqlLbl.Text = SelectString
    Dim mycnn As SqlConnection
    mycnn = New SqlConnection("user id=nsclinet; password=swa ;Initial
    Catalog=nstrack; Data Source=Carbon")

    Dim mycmd As SqlDataAdapter
    mycmd = New SqlDataAdapter(SelectString, mycnn)

    '// Create and fill a DataSet.
    Dim ds As DataSet
    ds = New DataSet
    mycmd.Fill(ds)

    Dim Mysource As DataView
    Mysource = New DataView(ds.Tables(0))
    DataGrid1.DataSource = Mysource
    DataGrid1.DataBind()

    now How do I add the total of each column adding a new row ?

    Thanks
    Carlos, Jun 9, 2004
    #1
    1. Advertising

  2. Hi Carlos,

    Look for the code in this article:

    HOW TO: Create a Summary Row for a DataGrid in ASP.NET by Using Visual Basic
    ..NET

    "The ItemDataBound event is raised after an item is data bound to the
    DataGrid control. This event gives you with the last opportunity to access
    the data item before it appears on the client. After this event is raised,
    the data item is null and is no longer available."

    http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q313154

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemDataBound
    Select Case e.Item.ItemType
    Case ListItemType.AlternatingItem, ListItemType.Item
    'Calculate total for the field of each row and alternating row.
    myTotal += CDbl(e.Item.Cells(2).Text)
    'Format the data, and then align the text of each cell to the
    right.
    e.Item.Cells(2).Text = Format(CDbl(e.Item.Cells(2).Text),
    "##,##0.00")
    e.Item.Cells(2).Attributes.Add("align", "right")
    Case ListItemType.Footer
    'Use the footer to display the summary row.
    e.Item.Cells(1).Text = "Total Sales"
    e.Item.Cells(1).Attributes.Add("align", "left")
    e.Item.Cells(2).Attributes.Add("align", "right")
    e.Item.Cells(2).Text = myTotal.ToString("c")
    End Select
    End Sub

    "Carlos" <> wrote in message
    news:...
    >I need to add the total of each column at the end of the grdi How can I do
    > that, here is my actual code to retreive the data.
    >
    >
    >
    > Dim SelectString As String
    > '//Convert date from string to Date
    > Dim L_Year As String
    > Dim L_Site As String
    > Dim L_Month As String
    > L_Site = Str(Val(Request.QueryString("TSiteNumber")) + 1)
    > L_Month = Request.QueryString("TMonths")
    > L_Year = Request.QueryString("TYear")
    >
    > SelectString = "SELECT TSteID, TMine, TDate, TTime, TGrade, TSignal FROM
    > Grade WHERE (YEAR(Tdate)=" + L_Year + ")" + " AND (MONTH(TDATE)=" +
    > L_Month
    > + ")" + " AND TSteID=" + L_Site + " ORDER BY TMine"
    >
    > '***************
    >
    > SqlLbl.Text = SelectString
    > Dim mycnn As SqlConnection
    > mycnn = New SqlConnection("user id=nsclinet; password=swa ;Initial
    > Catalog=nstrack; Data Source=Carbon")
    >
    > Dim mycmd As SqlDataAdapter
    > mycmd = New SqlDataAdapter(SelectString, mycnn)
    >
    > '// Create and fill a DataSet.
    > Dim ds As DataSet
    > ds = New DataSet
    > mycmd.Fill(ds)
    >
    > Dim Mysource As DataView
    > Mysource = New DataView(ds.Tables(0))
    > DataGrid1.DataSource = Mysource
    > DataGrid1.DataBind()
    >
    > now How do I add the total of each column adding a new row ?
    >
    > Thanks
    >
    >
    >
    >
    >
    >
    >
    Ken Cox [Microsoft MVP], Jun 9, 2004
    #2
    1. Advertising

  3. Check out this article,
    http://www.extremeexperts.com/Net/Articles/AddingControlstoFooter.aspx

    --
    Saravana
    Microsoft MVP - ASP.NET
    www.extremeexperts.com



    "Carlos" <> wrote in message
    news:...
    > I need to add the total of each column at the end of the grdi How can I do
    > that, here is my actual code to retreive the data.
    >
    >
    >
    > Dim SelectString As String
    > '//Convert date from string to Date
    > Dim L_Year As String
    > Dim L_Site As String
    > Dim L_Month As String
    > L_Site = Str(Val(Request.QueryString("TSiteNumber")) + 1)
    > L_Month = Request.QueryString("TMonths")
    > L_Year = Request.QueryString("TYear")
    >
    > SelectString = "SELECT TSteID, TMine, TDate, TTime, TGrade, TSignal FROM
    > Grade WHERE (YEAR(Tdate)=" + L_Year + ")" + " AND (MONTH(TDATE)=" +

    L_Month
    > + ")" + " AND TSteID=" + L_Site + " ORDER BY TMine"
    >
    > '***************
    >
    > SqlLbl.Text = SelectString
    > Dim mycnn As SqlConnection
    > mycnn = New SqlConnection("user id=nsclinet; password=swa ;Initial
    > Catalog=nstrack; Data Source=Carbon")
    >
    > Dim mycmd As SqlDataAdapter
    > mycmd = New SqlDataAdapter(SelectString, mycnn)
    >
    > '// Create and fill a DataSet.
    > Dim ds As DataSet
    > ds = New DataSet
    > mycmd.Fill(ds)
    >
    > Dim Mysource As DataView
    > Mysource = New DataView(ds.Tables(0))
    > DataGrid1.DataSource = Mysource
    > DataGrid1.DataBind()
    >
    > now How do I add the total of each column adding a new row ?
    >
    > Thanks
    >
    >
    >
    >
    >
    >
    >
    Saravana [MVP], Jun 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    adding totals in gridview rows

    John, Jun 9, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    593
  2. =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=

    Running totals in gridview (when you have many totals required)

    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 15, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    844
    Alexey Smirnov
    Feb 16, 2007
  3. SStory

    Totals in grid footer?

    SStory, May 11, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    153
    Saravana [MVP]
    May 14, 2004
  4. Mike the Canadian

    Generating totals of grid on load

    Mike the Canadian, Nov 17, 2005, in forum: Javascript
    Replies:
    3
    Views:
    102
    Joshie Surber
    Nov 19, 2005
  5. Mark Scott

    adding totals from 1 array to another

    Mark Scott, Feb 24, 2008, in forum: Javascript
    Replies:
    1
    Views:
    80
Loading...

Share This Page