SubTotal in GridView
To display Sub Totals in a GridView is a situation that arise frequently.
Here's a sample on how to do it using GridView.
First we need a table TBL_Patients. Here's the structure
Now lets populate some sample values to it.
The ASPX
<asp:GridView ID="gvwPatientBills" runat="server"
AutoGenerateColumns="false"
DataKeyNames="BillID"
GridLines="Both"
OnRowDataBound="gvwPatientBills_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Bill ID">
<ItemTemplate>
<asp:Label ID="lblBillID" runat="server"
Text='<%# Eval("BillID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient ID">
<ItemTemplate>
<asp:Label ID="lblPatientID" runat="server"
Text='<%# Eval("PatientID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient Name">
<ItemTemplate>
<asp:Label ID="lblPatientName" runat="server"
Text='<%# Eval("PatientName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient Ward">
<ItemTemplate>
<asp:Label ID="lblPatientWard" runat="server"
Text='<%# Eval("PatientWard") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill Amount">
<ItemTemplate>
<asp:Label ID="lblBillAmount" runat="server"
Text='<%# Eval("BillAmount") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Black" ForeColor="White" />
</asp:GridView>
The ASPX.CS
int iPatientIDCount = 0;
int iRowsCount = 0;
int iAddBills = 0;
string sPatientID = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
DataTable dtPatientBills = new DataTable();
string strSelectCommand = "SELECT * FROM TBL_PatientBills ORDER BY PatientID";
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
using (SqlDataAdapter adapPatientBills = new SqlDataAdapter(strSelectCommand, sqlConn))
{
adapPatientBills.Fill(dtPatientBills);
}
}
iRowsCount = dtPatientBills.Rows.Count - 1;
gvwPatientBills.DataSource = dtPatientBills;
gvwPatientBills.DataBind();
}
protected void gvwPatientBills_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sName = "";
if (e.Row.RowIndex == 0)
{
sName = ((Label)e.Row.FindControl("lblPatientName")).Text;
sPatientID = ((Label)e.Row.FindControl("lblPatientID")).Text;
iAddBills = Convert.ToInt32(((Label)e.Row.FindControl("lblBillAmount")).Text);
}
else
{
sName = ((Label)gvwPatientBills.Rows[e.Row.RowIndex - 1].FindControl("lblPatientName")).Text;
if (sPatientID == ((Label)e.Row.FindControl("lblPatientID")).Text)
{
iPatientIDCount = iPatientIDCount + 1;
iAddBills += Convert.ToInt32(((Label)e.Row.FindControl("lblBillAmount")).Text);
}
else
{
iPatientIDCount = iPatientIDCount + 1;
sPatientID = ((Label)e.Row.FindControl("lblPatientID")).Text;
Table tblTemp = (Table)this.gvwPatientBills.Controls[0];
int intIndex = tblTemp.Rows.GetRowIndex(e.Row);
GridViewRow gvrTemp = new GridViewRow(intIndex, intIndex, DataControlRowType.Separator, DataControlRowState.Normal);
TableCell cellTemp = new TableCell();
cellTemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#8FD8D8");
cellTemp.Font.Bold = true;
cellTemp.ColumnSpan = gvwPatientBills.Columns.Count;
cellTemp.HorizontalAlign = HorizontalAlign.Left;
cellTemp.Text = (iPatientIDCount != 1) ? "Total for " + sName + " in " + iPatientIDCount + " Entries: " + iAddBills : "";
cellTemp.Height = Unit.Pixel(20);
gvrTemp.Cells.Add(cellTemp);
tblTemp.Controls.AddAt(intIndex, gvrTemp);
iPatientIDCount = 0;
iAddBills = Convert.ToInt32(((Label)e.Row.FindControl("lblBillAmount")).Text); ;
}
if (iRowsCount == e.Row.RowIndex)
{
iPatientIDCount = iPatientIDCount + 1;
Table tblTemp = (Table)this.gvwPatientBills.Controls[0];
int intIndex = tblTemp.Rows.GetRowIndex(e.Row);
GridViewRow gvrTemp = new GridViewRow(intIndex + 1, intIndex + 1, DataControlRowType.Separator, DataControlRowState.Normal);
TableCell cellTemp = new TableCell();
cellTemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#8FD8D8");
cellTemp.Font.Bold = true;
cellTemp.ColumnSpan = gvwPatientBills.Columns.Count;
cellTemp.HorizontalAlign = HorizontalAlign.Left;
cellTemp.Text = (iPatientIDCount != 1) ? "Total for " + sName + " in " + iPatientIDCount + " Entries: " + iAddBills : "";
cellTemp.Height = Unit.Pixel(20);
gvrTemp.Cells.Add(cellTemp);
tblTemp.Controls.AddAt(intIndex + 1, gvrTemp);
iPatientIDCount = 0;
iAddBills = 0;
}
}
}
}
}
And the final result looks like this.
NB:
1. This code needs serious optimisation.
2. Its a no-no if you wanna implement Paging and Sorting here.For that we have to use the wonderful third party
GridView controls like Teleriks RadGrid and all