Set grideviw
property
<asp:GridView
ID="GridView1" runat="server"
AutoGenerateColumns="False"
BackColor="White"
BorderColor="#336699" BorderStyle="Solid"
BorderWidth="1px"
CellPadding="2"
Font-Names="Verdana" ShowFooter="true"
Font-Size="10pt"
Width="50%"
DataKeyNames="id1" GridLines="Horizontal"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="id1" HeaderText="ino"
/>
<asp:TemplateField HeaderText="total">
<ItemTemplate>
<asp:Label
ID="lblPrice" runat="server" Text='<%# Eval("total")%>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label
ID="lblTotalPrice" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
C# code :
private void BindData()
{
string cs =
@"connection string";
cn = new
SqlConnection(cs);
cn.Open();
string k = "select *
from invoicetbl";
SqlDataAdapter da = new
SqlDataAdapter(k,cn);
DataTable table = new
DataTable();
da.Fill(table);
GridView1.DataSource =
table;
GridView1.DataBind();
}
decimal totalPrice = 0M;
decimal totalStock = 0M;
int totalItems = 0;
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==
DataControlRowType.DataRow)
{
Label lblPrice =
(Label)e.Row.FindControl("lblPrice");
Label lblUnitsInStock
= (Label)e.Row.FindControl("lblUnitsInStock");
decimal price =
Decimal.Parse(lblPrice.Text);
totalPrice += price;
totalItems += 1;
}
if (e.Row.RowType ==
DataControlRowType.Footer)
{
Label lblTotalPrice =
(Label)e.Row.FindControl("lblTotalPrice");
lblTotalPrice.Text =
totalPrice.ToString();
}
}
No comments:
Post a Comment