Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
i developing report system in vb.net,i want to show the total amount in between gridview cells,
i try to many way but the result is null please help me..
my code and result is below..

VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
 Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Programs\sathish\invoicedb.mdb")
       Dim cmd As OleDbCommand = New OleDbCommand("SELECT productname,invoiceid,Clientname,invoicedate,totalamount,Amountpaid,balanceamount from invoice", con)
       con.Open()
       Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
       Dim myDataSet As DataSet = New DataSet()
       myDA.Fill(myDataSet, "invoice")
       DataGridView1.DataSource = myDataSet.Tables("invoice").DefaultView


VB
Dim s As Decimal
      s = 0
      For Each row As DataGridViewRow In DataGridView1.Rows
          If row.Cells(4).Value = Nothing Then
          Else
             s = s + Decimal.Parse(row.Cells(4).Value.ToString())
          End If
      Next
      DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(4).Value = s.ToString()
      con.Close()
      con = Nothing


XML
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
       If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then

           Using gridBrush As Brush = New SolidBrush(Me.DataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)

               Using gridLinePen As Pen = New Pen(gridBrush)

                   e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
                   If e.RowIndex < DataGridView1.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
                       e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                   End If
                   e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
                   If Not e.Value Is Nothing Then
                       If e.RowIndex > 0 AndAlso DataGridView1.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
                       Else
                           e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
                       End If
                   End If
                   e.Handled = True
               End Using
           End Using
       End If
   End Sub





and the result is:
---------------------------------
PRODUCT NAME  |   TOTAL AMOUNT
---------------------------------
S/W           |    5000
              |    2000
              |    3000
WEB HOSTING   |    1000
              |    2000

                  13000(This is sum of TOTAL AMOUNT)
---------------------------------


But i want:

---------------------------------
PRODUCT NAME  |   TOTAL AMOUNT
---------------------------------
S/W           |    5000
              |    2000
              |    3000
               
                   10000(This is sum of S/W AMOUNT)
  
WEB HOSTING   |    1000
              |    2000

                  3000(This is sum of WEB HOSTING AMOUNT)
---------------------------------

Pls send your ideas...
Posted
Updated 12-Aug-13 3:13am
v2

1 solution

All what you need to do is to write proper query:
SQL
SELECT productname, totalamount
FROM(
    SELECT productname, totalamount
    FROM invoice
    UNION ALL
    SELECT productname + ' - Total:' AS productname, SUM(totalamount) AS totalamount
    FROM invoice
    GROUP BY productname + ' - Total:'
) AS T
ORDER BY productname
 
Share this answer
 
Comments
sv sathish 13-Aug-13 0:56am    
thank you soooooo much sir.
Maciej Los 13-Aug-13 1:41am    
You're welcome ;)
sv sathish 13-Aug-13 4:36am    
sir how to use date time picker in this code. i want display the total amount in between two dates using date time picker.
Maciej Los 13-Aug-13 4:41am    
If you have any date field in your database, then
SELECT ...
FROM ...
WHERE DateField BETWENN [DateTimePickers1.Value] And [DateTimePickers2.Value]
sv sathish 13-Aug-13 4:51am    
i already try it sir and the code is below:
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Programs\sathish\invoicedb.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT productname, Totalamount,Amountpaid,BalanceAmount where invoicedate between " & DateTimePicker1.Value.Date() & " and " & DateTimePicker2.Value.Date() & " FROM (SELECT productname,Totalamount,amountpaid,BalanceAmount FROM invoice UNION ALL SELECT productname + ' - Total:' AS productname, SUM(totalamount) AS Totalamount,SUM(amountpaid) AS Amountpaid,SUM(BalanceAmount) As BalanceAmount FROM invoice GROUP BY productname + ' - Total:') AS T ORDER BY productname ", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "invoice")
DataGridView1.DataSource = myDataSet.Tables("invoice").DefaultView


i get the error in this methode, the error is " Syntax error (missing operator) in query expression 'BalanceAmount where invoicedate between 13-08-2000 and 13-08-2013'. "

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900