adding totals to grid

C

Carlos

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
 
K

Ken Cox [Microsoft MVP]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top