ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Update quantity if item already exists else add new row in ASP.Net GridView using C# and VB.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">Qty : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtqty&#34; /&gt; Description : &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlItems&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Insert&#34; /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34; OnRowEditing=&#34;GridView1_RowEditing&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Descriptionitem&#34; HeaderText=&#34;Descriptionitem&#34; /&gt; &lt;asp:BoundField DataField=&#34;QTY&#34; HeaderText=&#34;QTY&#34; /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lnkEdit&#34; Text=&#34;Edit&#34; runat=&#34;server&#34; CommandName=&#34;Edit&#34; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lnkUpdate&#34; Text=&#34;Update&#34; runat=&#34;server&#34; OnClick=&#34;OnUpdate&#34; /&gt; &lt;asp:LinkButton ID=&#34;lnkCancel&#34; Text=&#34;Cancel&#34; runat=&#34;server&#34; OnClick=&#34;OnCancel&#34; /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dtddl = new DataTable(); dtddl.Columns.Add(&#34;Descriptionitem&#34;); dtddl.Columns.Add(&#34;Codeitem&#34;); dtddl.Rows.Add(&#34;A&#34;, 1); dtddl.Rows.Add(&#34;B&#34;, 2); dtddl.Rows.Add(&#34;C&#34;, 3); ddlItems.DataSource = dtddl; ddlItems.DataTextField = &#34;Descriptionitem&#34;; ddlItems.DataValueField = &#34;Codeitem&#34;; ddlItems.DataBind(); ddlItems.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;0&#34;)); } } protected void BindGrid() { GridView1.DataSource = ViewState[&#34;dt&#34;] as DataTable; GridView1.DataBind(); } protected void Insert(object sender, EventArgs e) { bool ifExist = false; string item = ddlItems.SelectedItem.Text; int quantity = Convert.ToInt16(txtqty.Text); DataTable dt; if (ViewState[&#34;dt&#34;] == null) { dt = new DataTable(); dt.Columns.Add(&#34;Descriptionitem&#34;); dt.Columns.Add(&#34;Qty&#34;); dt.Rows.Add(item, quantity); ViewState[&#34;dt&#34;] = dt; } else { dt = ViewState[&#34;dt&#34;] as DataTable; for (int i = 0; i &lt; dt.Rows.Count; i++) { if (dt.Rows[i][&#34;Descriptionitem&#34;].ToString().ToLower() == item.ToLower()) { dt.Rows[i][&#34;QTY&#34;] = Convert.ToInt16(dt.Rows[i][&#34;QTY&#34;]) + quantity; ifExist = true; break; } } if (!ifExist) { dt.Rows.Add(item, quantity); } ViewState[&#34;dt&#34;] = dt; } GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; BindGrid(); } protected void OnUpdate(object sender, EventArgs e) { GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; string descriptionitem = (row.Cells[0].Controls[0] as TextBox).Text; int qty = Convert.ToInt32(((row.Cells[1].Controls[0] as TextBox).Text)); DataTable dt = ViewState[&#34;dt&#34;] as DataTable; dt.Rows[row.RowIndex][&#34;Descriptionitem&#34;] = descriptionitem; dt.Rows[row.RowIndex][&#34;QTY&#34;] = qty; ViewState[&#34;dt&#34;] = dt; GridView1.EditIndex = -1; BindGrid(); } protected void OnCancel(object sender, EventArgs e) { GridView1.EditIndex = -1; BindGrid(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dtddl As DataTable = New DataTable() dtddl.Columns.Add(&#34;Descriptionitem&#34;) dtddl.Columns.Add(&#34;Codeitem&#34;) dtddl.Rows.Add(&#34;A&#34;, 1) dtddl.Rows.Add(&#34;B&#34;, 2) dtddl.Rows.Add(&#34;C&#34;, 3) ddlItems.DataSource = dtddl ddlItems.DataTextField = &#34;Descriptionitem&#34; ddlItems.DataValueField = &#34;Codeitem&#34; ddlItems.DataBind() ddlItems.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;0&#34;)) End If End Sub Protected Sub BindGrid() GridView1.DataSource = TryCast(ViewState(&#34;dt&#34;), DataTable) GridView1.DataBind() End Sub Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Dim ifExist As Boolean = False Dim item As String = ddlItems.SelectedItem.Text Dim quantity As Integer = Convert.ToInt16(txtqty.Text) Dim dt As DataTable If ViewState(&#34;dt&#34;) Is Nothing Then dt = New DataTable() dt.Columns.Add(&#34;Descriptionitem&#34;) dt.Columns.Add(&#34;Qty&#34;) dt.Rows.Add(item, quantity) ViewState(&#34;dt&#34;) = dt Else dt = TryCast(ViewState(&#34;dt&#34;), DataTable) For i As Integer = 0 To dt.Rows.Count - 1 If dt.Rows(i)(&#34;Descriptionitem&#34;).ToString().ToLower() = item.ToLower() Then dt.Rows(i)(&#34;QTY&#34;) = Convert.ToInt16(dt.Rows(i)(&#34;QTY&#34;)) + quantity ifExist = True Exit For End If Next If Not ifExist Then dt.Rows.Add(item, quantity) End If ViewState(&#34;dt&#34;) = dt End If GridView1.DataSource = dt GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) GridView1.EditIndex = e.NewEditIndex BindGrid() End Sub Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs) Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow) Dim descriptionitem As String = (TryCast(row.Cells(0).Controls(0), TextBox)).Text Dim qty As Integer = Convert.ToInt32(((TryCast(row.Cells(1).Controls(0), TextBox)).Text)) Dim dt As DataTable = TryCast(ViewState(&#34;dt&#34;), DataTable) dt.Rows(row.RowIndex)(&#34;Descriptionitem&#34;) = descriptionitem dt.Rows(row.RowIndex)(&#34;QTY&#34;) = qty ViewState(&#34;dt&#34;) = dt GridView1.EditIndex = -1 BindGrid() End Sub Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs) GridView1.EditIndex = -1 BindGrid() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/u6f3msk.gif" alt="" width="254" height="255" /></p>https://www.aspforums.net:443/Threads/122612/Update-quantity-if-item-already-exists-else-add-new-row-in-ASPNet-GridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/122612/Update-quantity-if-item-already-exists-else-add-new-row-in-ASPNet-GridView-using-C-and-VBNet/Fri, 09 Aug 2019 03:11:56 GMT