Ok, so your smple data has 3 months. (of course they are all the same - no doubt just for this example and testing).
So, drop a gridview on your page.
So, we have this:
And then in code behind, we can add a group by "month" to allow totals and a row for each month.
So, this code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Dim strSQL As String =
"SELECT month(date) As MyMonth, sum (Opening) as Opening, sum (Purchase) as Purchase,
sum (Stk_Tfr_In) as Stk_Tfr_In,sum (Stk_Tfr_Out) as Stk_tfr_Out,
sum (Damaged) as Damaged,sum (StoreOut) as StoreOut,sum (Closing_Stk) as Closing_Stk
FROM data1
WHERE MONTH(DATE) in (9,10,11)
group by month(date)"
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
GridView1.DataSource = cmdSQL.ExecuteReader()
GridView1.DataBind()
End Using
End Using
And now the results are this:
so you can add a group-by month, and it will split out the totals for each month.
if you want a sum (footer), then to gv, add ShowFooter="true"
and then this:
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstData
GridView1.DataBind()
End Using
End Using
Dim gF = GridView1.FooterRow
gF.Cells(0).Text = "Totals"
For i = 1 To rstData.Columns.Count - 1
gF.Cells(i).Text = rstData.Compute("SUM(" & rstData.Columns(i).ColumnName & ")", "")
Next
And if you want all cells right justifed, then add a item row bound like this:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Or
e.Row.RowType = DataControlRowType.Footer Then
For i = 1 To rstData.Columns.Count - 1
e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
Next
End If
End Sub
And now you have this:
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta, Canada