Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grandtotal for datagrid

Status
Not open for further replies.

smithbr

MIS
May 29, 2003
85
0
0
US
Hello again,
I have read that it is not possible to get a grand total ina datagrid. (adding all the rows of a particular column) I was wondering though, is it possible to find teh grand total another way an display it in a label somewhere on the form. The column I want to total is one that I create at run time. Here is the code I use to create the "Total" row. What code do I need to total all the rows within the column?


OleDbDataAdapter1.SelectCommand.Parameters(0).Value = txtinvnum.Text
OleDbDataAdapter1.Fill(DSInvoice1)
Try
DSInvoice1.Tables(0).Columns.Remove("Total")
Catch
End Try

DSInvoice1.Tables(0).Columns.Add("Total")
Dim dr As DataRow

If rdoyes.Checked Then
For Each dr In DSInvoice1.Tables(0).Rows
dr.Item("Total") = ((dr.Item("QuantityShipped") * dr.Item("LastUnitPrice")) * (1 - dr.Item("DiscountP")))
Next
grdcomm.DataSource = Nothing
grdcomm.SetDataBinding(DsInvoice1, "Invoicehistory")

ElseIf rdono.Checked Then
For Each dr In DSInvoice1.Tables(0).Rows
dr.Item("Total") = dr.Item("QuantityShipped") * dr.Item("LastUnitPrice")

Next
grdcomm.DataSource = Nothing
grdcomm.SetDataBinding(DsInvoice1, "Invoicehistory")
End If
 
I has hoping to create the grand total in a label....not in the datagrid at all.
 

try this !!!

Dim i As Integer
Dim total As Integer

For i = 0 To objDS.Tables(0).Rows.Count - 1
total = total + objDS.Tables(0).Rows(i).Item(0)
Next

yourLabel.Text = total

where .Item(0) is the column for which you want the grand total

 
hat I do is, first I save the detail line which has the Order Amount in the Order table.
Then use the Sum function in a query to get the total and display on the text box or label.


Private Sub FindInvoiceTotal(ByVal InvoiceKey As String)

Dim strSQL As String
Try
' Build Select statement to query Detail information from the Order_Invoice
' table
strSQL = "SELECT Invoice.InvoiceNumber," & _
" SUM(Order_Detail.Amount)AS InvoiceTotal" & _
" FROM (Invoice INNER JOIN Order_Detail ON" & _
" Invoice.InvoiceNumber = ORder_Detail.InvoiceNumber" & _
" WHERE Invoice.InvoiceNumber = " & InvoiceNumber & _
" GROUP BY Invoice.InvoiceNumber"

Dim myConnection As OleDbConnection = New OleDbConnection(ConnectionString)
myConnection.Open()
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
Dim Reader As OleDbDataReader = myCommand.ExecuteReader()

If Reader.Read() Then
' Populate form with the data
txtInvoiceAmt.Text() = Reader.Item("InvoiceTotal").ToString()
Else
txtInvoiceAmt.Text() = "0.00"
End If
' Close and Clean up objects
Reader.Close()
myConnection.Close()
myCommand.Dispose()

Catch e As OleDbException
MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")

Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top