且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在依赖产品名称中显示总金额

更新时间:2023-11-30 12:29:34

所有你需要的东西做的是写出正确的查询:

  SELECT  productname,totalamount 
FROM
SELECT productname,totalamount
FROM invoice
UNION ALL
SELECT productname + ' - 总计:' AS productname,SUM(totalamount) AS totalamount
FROM invoice
GROUP BY productname + ' - 总计:'
AS T
ORDER BY productname


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..

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



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



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...

All what you need to do is to write proper query:
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