Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="padding-bottom: 10px">
Name:<br />
<asp:DropDownList ID="ddlItems" runat="server" Width="150px">
<asp:ListItem Text="Select" Value="0" />
<asp:ListItem Text="Apple" Value="Apple" />
<asp:ListItem Text="Mango" Value="Mango" />
<asp:ListItem Text="Orange" Value="Orange" />
<asp:ListItem Text="Banana" Value="Banana" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="padding-bottom: 10px">
Quantity:<br />
<asp:TextBox ID="txtQuantity" runat="server" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
EmptyDataText="No records has been added.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[2] {
new System.Data.DataColumn("Name"), new System.Data.DataColumn("Quantity") });
ViewState["Customers"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
GridView1.DataSource = (System.Data.DataTable)ViewState["Customers"];
GridView1.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
System.Data.DataTable dt = (System.Data.DataTable)ViewState["Customers"];
if (ddlItems.SelectedIndex > 0 && txtQuantity.Text.Trim() != "")
{
System.Data.DataRow[] dr = dt.Select("Name='" + ddlItems.SelectedValue.Trim() + "'");
if (dr.Length > 0)
{
dr[0]["Quantity"] = Convert.ToInt32(dr[0]["Quantity"]) + Convert.ToInt32(txtQuantity.Text.Trim());
dr[0].AcceptChanges();
}
else
{
dt.Rows.Add(ddlItems.SelectedValue.Trim(), txtQuantity.Text.Trim());
}
ViewState["Customers"] = dt;
this.BindGrid();
ddlItems.SelectedIndex = 0;
txtQuantity.Text = string.Empty;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(1) {
New System.Data.DataColumn("Name"), New System.Data.DataColumn("Quantity")})
ViewState("Customers") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
GridView1.DataSource = CType(ViewState("Customers"), System.Data.DataTable)
GridView1.DataBind()
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As System.Data.DataTable = CType(ViewState("Customers"), System.Data.DataTable)
If ddlItems.SelectedIndex > 0 AndAlso txtQuantity.Text.Trim() <> "" Then
Dim dr As System.Data.DataRow() = dt.[Select]("Name='" & ddlItems.SelectedValue.Trim() & "'")
If dr.Length > 0 Then
dr(0)("Quantity") = Convert.ToInt32(dr(0)("Quantity")) + Convert.ToInt32(txtQuantity.Text.Trim())
dr(0).AcceptChanges()
Else
dt.Rows.Add(ddlItems.SelectedValue.Trim(), txtQuantity.Text.Trim())
End If
ViewState("Customers") = dt
Me.BindGrid()
ddlItems.SelectedIndex = 0
txtQuantity.Text = String.Empty
End If
End Sub
Screenshot