Hi sat,
I have created a sample which full fill your requirement instead of binding textbox you need to bind label
HTML
<div align="center">
Quantity:-
<asp:TextBox ID="lblQuantity" Text="0" OnTextChanged="ValueChange" AutoPostBack="true"
runat="server" />
<br />
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_DataBound"
AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="HourlyPrice" HeaderText="HourlyPrice" />
<asp:BoundField HeaderText="Qty" />
<asp:BoundField HeaderText="Sub-Total" />
</Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbAdditional" runat="server" OnCheckedChanged="CheckedItem" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Services" HeaderText="Services" />
<asp:BoundField DataField="Rate" HeaderText="Rate" />
</Columns>
</asp:GridView>
<br />
Grand Total=
<asp:Label ID="lblGrandTotal" runat="server" />
</div>
C#
decimal total = 0;
decimal grandTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
lblGrandTotal.Text = grandTotal.ToString();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("HourlyPrice") });
dt.Rows.Add(1, "Derek Tsong", 221);
dt.Rows.Add(2, "Dolma Mari", 195);
dt.Rows.Add(3, "Ernet Toko", 251);
dt.Rows.Add(4, "Pelermo Ivy", 350);
dt.Rows.Add(5, "Sunil Verma", 290);
dt.Rows.Add(6, "Lavon Tyagi", 150);
GridView1.DataSource = dt;
GridView1.DataBind();
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Services"), new DataColumn("Rate") });
dt1.Rows.Add(1, "LOC123", 25);
dt1.Rows.Add(2, "LOC456", 50);
dt1.Rows.Add(3, "LOC789", 75);
GridView2.DataSource = dt1;
GridView2.DataBind();
}
protected void ValueChange(object sender, EventArgs e)
{
BindGrid();
}
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Text = lblQuantity.Text;
e.Row.Cells[4].Text = (Convert.ToInt32(lblQuantity.Text) * Convert.ToInt32(e.Row.Cells[2].Text)).ToString();
total = total + Convert.ToDecimal(e.Row.Cells[4].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "Total";
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[4].Text = total.ToString("N2");
lblGrandTotal.Text = e.Row.Cells[4].Text;
}
}
protected void CheckedItem(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("cbAdditional");
if (cb.Checked)
{
grandTotal = grandTotal + Convert.ToDecimal(row.Cells[3].Text);
}
}
if (grandTotal > 0)
{
lblGrandTotal.Text = (grandTotal + Convert.ToDecimal(GridView1.FooterRow.Cells[4].Text)).ToString();
}
else
{
lblGrandTotal.Text = GridView1.FooterRow.Cells[4].Text.ToString();
}
}
ScreenShot
