Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<div class="auto-style1">
<h1>
<strong>BigBale Dispatch </strong>
</h1>
</div>
<table class="auto-style2">
<tr>
<td class="auto-style7">
Bale No Input :
</td>
<td class="auto-style8">
<asp:TextBox ID="txtsearcbn" runat="server" Height="24px" Width="127px"></asp:TextBox>
<asp:DropDownList ID="DDLitem" runat="server">
<asp:ListItem Value="1">Cut wool Sweaters 60-100%</asp:ListItem>
<asp:ListItem Value="2">White Cotton Shirts</asp:ListItem>
<asp:ListItem Value="3">Mens Cotton Shirts</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtqty" runat="server" Height="16px" Width="18px"></asp:TextBox>
<asp:TextBox ID="txtwt" runat="server" Height="16px" Width="18px"></asp:TextBox>
<%--<asp:Button ID="btnadd" runat="server" Text="ADD" Visible="false" OnClick="btnadd_Click" />--%>
</td>
<td class="auto-style9">
</td>
</tr>
<tr>
<td class="auto-style5">
</td>
<td class="auto-style6">
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style5">
</td>
<td class="auto-style6">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnadd_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style5">
</td>
<td class="auto-style6">
<asp:GridView ID="GVbal" runat="server" OnRowDeleting="GVbal_RowDeleting" AutoGenerateColumns="false"
DataKeyNames="Index">
<Columns>
<asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle CssClass="table_04" HorizontalAlign="Left"></HeaderStyle>
<ItemStyle CssClass="table_02" HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bale_No">
<ItemTemplate>
<asp:Label ID="BID" runat="server" Text='<%#Bind("BID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="Descriptionitem" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:Label ID="SBWeight" runat="server" Text='<%#Bind("BWeight")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
</td>
<td>
<asp:Label ID="lblSummary" Text="Sumary GridView" runat="server" /><br />
<asp:GridView runat="server" ID="gvSummary" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Sr No.">
<ItemTemplate>
<%# Container.DataItemIndex +1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%#Bind("Description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:Label ID="lblQTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:Label ID="lblBWeight" runat="server" Text='<%#Bind("Weight")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
Namespaces
C#
using System.Data;
using System.Linq;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Index", typeof(Int32)));
dt.Columns.Add(new DataColumn("BID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Codeitem", typeof(string)));
dt.Columns.Add(new DataColumn("Descriptionitem", typeof(string)));
dt.Columns.Add(new DataColumn("QTY", typeof(Int32)));
dt.Columns.Add(new DataColumn("BWeight", typeof(Int32)));
ViewState["dt"] = dt;
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
insertbalgv();
}
private void insertbalgv()
{
if (DDLitem.SelectedItem != null)
{
int codeitem = Convert.ToInt32(DDLitem.SelectedItem.Value.ToString());
string Descriptionitem = DDLitem.SelectedItem.Text.ToString();
DataTable dtgridview = ViewState["dt"] as DataTable;
DataRow dr2 = dtgridview.NewRow();
bool ifExist = false;
foreach (GridViewRow row in GVbal.Rows)
foreach (GridViewRow dr in GVbal.Rows)
{
string bId = dr.Cells[0].Text; // If BoundField pass the cell value.
//string bId = (dr.FindControl("BID") as Label).Text; // If TemplateField use FindControl.
if (bId == txtsearcbn.Text.Trim())
{
ifExist = true;
break;
}
}
if (!ifExist)
{
dr2["Index"] = dtgridview.Rows.Count + 1;
dr2["BID"] = txtsearcbn.Text.Trim();
dr2["Codeitem"] = codeitem;
dr2["Descriptionitem"] = Descriptionitem;
dr2["QTY"] = txtqty.Text;
dr2["BWeight"] = txtwt.Text;
dtgridview.Rows.Add(dr2);
ViewState["dt"] = dtgridview;
GVbal.DataSource = dtgridview;
GVbal.DataBind();
BindSummaryGrid(dtgridview);
}
else
{
// this.lbgvck.Visible = true;
Response.Write("<script language='javascript'>alert('Bale Already Added');</script>");
}
clear();
}
}
protected void BindSummaryGrid(DataTable dt)
{
DataTable dtSummary = new DataTable();
dtSummary.Columns.Add(new DataColumn("Description", typeof(string)));
dtSummary.Columns.Add(new DataColumn("QTY", typeof(int)));
dtSummary.Columns.Add(new DataColumn("Weight", typeof(int)));
var result = (from row in dt.AsEnumerable()
group row by new { Description = row["Descriptionitem"] } into g
select new
{
g.Key.Description,
QTY = g.Sum(x => Convert.ToDecimal(x["QTY"])),
Weight = g.Sum(x => Convert.ToDecimal(x["BWeight"]))
});
foreach (var item in result)
{
dtSummary.Rows.Add(item.Description, item.QTY, item.Weight);
}
gvSummary.DataSource = dtSummary;
gvSummary.DataBind();
}
protected void GVbal_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string index = GVbal.DataKeys[e.RowIndex].Values[0].ToString();
DataTable dt = ViewState["dt"] as DataTable;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["Index"].ToString() == index)
{
dt.Rows.Remove(dt.Rows[i]);
ViewState["dt"] = dt;
GVbal.DataSource = dt;
GVbal.DataBind();
BindSummaryGrid(dt);
break;
}
}
}
private void clear()
{
txtsearcbn.Text = "";
}
Screenshots