Hi rakeshkuma,
Check this example. Now please take its reference and correct your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
}
table th {
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td {
padding: 5px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:UpdatePanel ID="upnlAccordionPane2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<hr />
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Quanitity</th>
<th>Net</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<asp:TextBox ID="txtItem" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtQuanitity" runat="server" onkeyup="AddGetNetAmt(this);"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtNet" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Button ID="btnAdd" runat="server" Text="ADD" OnClick="Add" />
</td>
</tr>
</tbody>
</table>
<hr />
<asp:GridView ID="gv_poitem" ShowFooter="true" runat="server" AutoGenerateColumns="false" DataKeyNames="Item">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkDel" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item">
<ItemTemplate>
<asp:Label runat="server" ID="lblItem" Text='<% #Eval("Item") %>'></asp:Label>
<asp:TextBox ID="txtItem" runat="server" Visible="false" Text='<% #Eval("Item") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label runat="server" ID="lblPrice" Text='<% #Eval("Price") %>'></asp:Label>
<asp:TextBox ID="txtPrice" runat="server" Visible="false" Text='<% #Eval("Price") %>'
AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label runat="server" ID="lblQuantity" Text='<% #Eval("Quantity") %>'></asp:Label>
<asp:TextBox ID="txtQuantity" runat="server" CssClass="txtQuantity" Visible="false" Text='<% #Eval("Quantity") %>'
AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
Grand Total :
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net">
<ItemTemplate>
<asp:Label ID="lblNet" runat="server" Text='<% #Eval("Net") %>'></asp:Label>
<asp:TextBox ID="txtNet" runat="server" Text='<% #Eval("Net") %>' Visible="false" ReadOnly="true"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblGrandTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
<hr />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="Update" Visible="false" />
<asp:Button ID="btnDelete" runat="server" Text="DELETE" OnClientClick="return confirm('Are you sure to delete ?')" OnClick="Delete" Visible="false" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Code
C#
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
this.BindGrid();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Item"),
new DataColumn("Price"),
new DataColumn("Quantity"),
new DataColumn("Net") });
dt.Rows.Add("AAA", 2, 25, 50);
dt.Rows.Add("BBB", 3);
dt.Rows.Add("CCC", 8);
gv_poitem.DataSource = dt;
ViewState["SelectedRecords"] = dt;
gv_poitem.DataBind();
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = sender as CheckBox;
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in gv_poitem.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
CheckBox chkAll = gv_poitem.HeaderRow.FindControl("chkAll") as CheckBox;
chkAll.Checked = true;
foreach (GridViewRow row in gv_poitem.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 4; i <= row.Cells.Count - 1; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
if (isChecked && !isUpdateVisible)
isUpdateVisible = true;
if (!isChecked)
chkAll.Checked = false;
}
}
}
btnUpdate.Visible = isUpdateVisible;
btnDelete.Visible = isUpdateVisible;
CalculateGrandTotal();
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in gv_poitem.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
DataTable dt = ViewState["SelectedRecords"] as DataTable;
string Item = row.Cells[2].Controls.OfType<TextBox>().FirstOrDefault().Text;
string Price = row.Cells[3].Controls.OfType<TextBox>().FirstOrDefault().Text;
string Quantity = row.Cells[4].Controls.OfType<TextBox>().FirstOrDefault().Text;
string Net = row.Cells[5].Controls.OfType<TextBox>().FirstOrDefault().Text;
dt.Rows[row.RowIndex]["Item"] = Item;
dt.Rows[row.RowIndex]["Price"] = Price;
dt.Rows[row.RowIndex]["Quantity"] = Quantity;
dt.Rows[row.RowIndex]["Net"] = Net;
ViewState["SelectedRecords"] = dt;
gv_poitem.DataSource = dt;
gv_poitem.DataBind();
}
}
}
btnUpdate.Visible = false;
btnDelete.Visible = false;
}
protected void Delete(object sender, EventArgs e)
{
DataTable dt = ViewState["SelectedRecords"] as DataTable;
for (int i = gv_poitem.Rows.Count - 1; i >= 0; i += -1)
{
GridViewRow row = gv_poitem.Rows[i];
CheckBox chkDelete = row.FindControl("chkDel") as CheckBox;
if (chkDelete.Checked)
dt.Rows[i].Delete();
}
ViewState["SelectedRecords"] = dt;
gv_poitem.DataSource = dt;
gv_poitem.DataBind();
btnUpdate.Visible = false;
btnDelete.Visible = false;
}
private DataTable TempTabel
{
get
{
DataTable dt = new DataTable();
if (ViewState["SelectedRecords"] == null)
{
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Item", typeof(string)),
new DataColumn("Price", typeof(string)),
new DataColumn("Quantity", typeof(string)),
new DataColumn("Net", typeof(string)) });
ViewState["SelectedRecords"] = dt;
}
return (DataTable)ViewState["SelectedRecords"];
}
set
{
ViewState["SelectedRecords"] = value;
}
}
protected void Add(object sender, EventArgs e)
{
DataTable dt = TempTabel;
if (TempTabel != null)
{
if (gv_poitem.Rows.Count == 0)
{
dt.Rows.Add(txtItem.Text, txtPrice.Text, txtQuanitity.Text, txtNet.Text);
this.gv_poitem.DataSource = dt;
gv_poitem.DataBind();
}
else
{
DataRow NewRow = dt.NewRow();
NewRow[0] = txtItem.Text;
NewRow[1] = txtPrice.Text;
NewRow[2] = txtQuanitity.Text;
NewRow[3] = txtNet.Text;
dt.Rows.Add(NewRow);
this.gv_poitem.DataSource = dt;
gv_poitem.DataBind();
}
}
TempTabel = dt;
}
protected void OnTextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
string price = (row.FindControl("txtPrice") as TextBox).Text;
string quantity = (row.FindControl("txtQuantity") as TextBox).Text;
TextBox txtNet = row.FindControl("txtNet") as TextBox;
price = !string.IsNullOrEmpty(price) ? price : "0";
quantity = !string.IsNullOrEmpty(quantity) ? quantity : "0";
txtNet.Text = (Convert.ToInt32(price) * Convert.ToInt32(quantity)).ToString();
CalculateGrandTotal();
}
private void CalculateGrandTotal()
{
var grandTotal = 0;
foreach (GridViewRow poRows in gv_poitem.Rows)
{
if ((poRows.FindControl("chkDel") as CheckBox).Checked)
{
string net = (poRows.FindControl("txtNet") as TextBox).Text;
net = !string.IsNullOrEmpty(net) ? net : "0";
grandTotal += Convert.ToInt32(net);
}
}
(gv_poitem.FooterRow.FindControl("lblGrandTotal") as Label).Text = grandTotal.ToString();
}
VB.Net
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {
New DataColumn("Item"),
New DataColumn("Price"),
New DataColumn("Quantity"),
New DataColumn("Net")})
dt.Rows.Add("AAA", 2, 25, 50)
dt.Rows.Add("BBB", 3)
dt.Rows.Add("CCC", 8)
gv_poitem.DataSource = dt
ViewState("SelectedRecords") = dt
gv_poitem.DataBind()
End Sub
Protected Sub OnCheckedChanged(sender As Object, e As EventArgs)
Dim isUpdateVisible As Boolean = False
Dim chk As CheckBox = TryCast(sender, CheckBox)
If chk.ID = "chkAll" Then
For Each row As GridViewRow In gv_poitem.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
End If
Next
End If
Dim chkAll As CheckBox = TryCast(gv_poitem.HeaderRow.FindControl("chkAll"), CheckBox)
chkAll.Checked = True
For Each row As GridViewRow In gv_poitem.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 4 To row.Cells.Count - 1
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
End If
If isChecked AndAlso Not isUpdateVisible Then
isUpdateVisible = True
End If
If Not isChecked Then
chkAll.Checked = False
End If
Next
End If
Next
btnUpdate.Visible = isUpdateVisible
btnDelete.Visible = isUpdateVisible
CalculateGrandTotal()
End Sub
Protected Sub Update(sender As Object, e As EventArgs)
For Each row As GridViewRow In gv_poitem.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim dt As DataTable = TryCast(ViewState("SelectedRecords"), DataTable)
Dim Item As String = row.Cells(2).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim Price As String = row.Cells(3).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim Quantity As String = row.Cells(4).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim Net As String = row.Cells(5).Controls.OfType(Of TextBox)().FirstOrDefault().Text
dt.Rows(row.RowIndex)("Item") = Item
dt.Rows(row.RowIndex)("Price") = Price
dt.Rows(row.RowIndex)("Quantity") = Quantity
dt.Rows(row.RowIndex)("Net") = Net
ViewState("SelectedRecords") = dt
gv_poitem.DataSource = dt
gv_poitem.DataBind()
End If
End If
Next
btnUpdate.Visible = False
btnDelete.Visible = False
End Sub
Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(ViewState("SelectedRecords"), DataTable)
For i As Integer = gv_poitem.Rows.Count - 1 To 0 Step -1
Dim row As GridViewRow = gv_poitem.Rows(i)
Dim chkDelete As CheckBox = TryCast(row.FindControl("chkDel"), CheckBox)
If chkDelete.Checked Then
dt.Rows(i).Delete()
End If
Next
ViewState("SelectedRecords") = dt
gv_poitem.DataSource = dt
gv_poitem.DataBind()
btnUpdate.Visible = False
btnDelete.Visible = False
End Sub
Private Property TempTabel As DataTable
Get
Dim dt As DataTable = New DataTable()
If ViewState("SelectedRecords") Is Nothing Then
dt.Columns.AddRange(New DataColumn(3) {
New DataColumn("Item", GetType(String)),
New DataColumn("Price", GetType(String)),
New DataColumn("Quantity", GetType(String)),
New DataColumn("Net", GetType(String))})
ViewState("SelectedRecords") = dt
End If
Return CType(ViewState("SelectedRecords"), DataTable)
End Get
Set(ByVal value As DataTable)
ViewState("SelectedRecords") = value
End Set
End Property
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TempTabel
If TempTabel IsNot Nothing Then
If gv_poitem.Rows.Count = 0 Then
dt.Rows.Add(txtItem.Text,
txtPrice.Text,
txtQuanitity.Text,
txtNet.Text)
Me.gv_poitem.DataSource = dt
gv_poitem.DataBind()
Else
Dim NewRow As DataRow = dt.NewRow()
NewRow(0) = txtItem.Text
NewRow(1) = txtPrice.Text
NewRow(2) = txtQuanitity.Text
NewRow(3) = txtNet.Text
dt.Rows.Add(NewRow)
Me.gv_poitem.DataSource = dt
gv_poitem.DataBind()
End If
End If
TempTabel = dt
End Sub
Protected Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim price As String = (TryCast(row.FindControl("txtPrice"), TextBox)).Text
Dim quantity As String = (TryCast(row.FindControl("txtQuantity"), TextBox)).Text
Dim txtNet As TextBox = TryCast(row.FindControl("txtNet"), TextBox)
price = If(Not String.IsNullOrEmpty(price), price, "0")
quantity = If(Not String.IsNullOrEmpty(quantity), quantity, "0")
txtNet.Text = (Convert.ToInt32(price) * Convert.ToInt32(quantity)).ToString()
CalculateGrandTotal()
End Sub
Private Sub CalculateGrandTotal()
Dim grandTotal = 0
For Each poRows As GridViewRow In gv_poitem.Rows
If (TryCast(poRows.FindControl("chkDel"), CheckBox)).Checked Then
Dim net As String = (TryCast(poRows.FindControl("txtNet"), TextBox)).Text
net = If(Not String.IsNullOrEmpty(net), net, "0")
grandTotal += Convert.ToInt32(net)
End If
Next
TryCast(gv_poitem.FooterRow.FindControl("lblGrandTotal"), Label).Text = grandTotal.ToString()
End Sub