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.Set ASP.Net GridView row add limit based on DropDownList value 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:DropDownList ID=&#34;DDLPktype&#34; runat=&#34;server&#34;&gt; &lt;asp:ListItem Text=&#34;NinePack&#34; Value=&#34;9&#34; /&gt; &lt;asp:ListItem Text=&#34;SixPack&#34; Value=&#34;6&#34; /&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;Button1&#34; 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">DataTable dt = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add(&#34;Descriptionitem&#34;); dt.Columns.Add(&#34;Qty&#34;); 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;)); ViewState[&#34;dt&#34;] = dt; } } protected void BindGrid() { GridView1.DataSource = ViewState[&#34;dt&#34;] as DataTable; GridView1.DataBind(); } protected void Insert(object sender, EventArgs e) { dt = ViewState[&#34;dt&#34;] as DataTable; dr = dt.NewRow(); dr[&#34;Descriptionitem&#34;] = ddlItems.SelectedItem.Text; dr[&#34;QTY&#34;] = txtqty.Text; if (dt.Rows.Count &lt; Convert.ToInt16(DDLPktype.SelectedItem.Value)) { dt.Rows.Add(dr); } else { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;You can not insert more than &#34; + dt.Rows.Count + &#34; rows&#39;)&#34;, true); } 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">Private dt As DataTable = New DataTable() Private dr As DataRow Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then dt.Columns.Add(&#34;Descriptionitem&#34;) dt.Columns.Add(&#34;Qty&#34;) 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;)) ViewState(&#34;dt&#34;) = dt 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) dt = TryCast(ViewState(&#34;dt&#34;), DataTable) dr = dt.NewRow() dr(&#34;Descriptionitem&#34;) = ddlItems.SelectedItem.Text dr(&#34;QTY&#34;) = txtqty.Text If dt.Rows.Count &lt; Convert.ToInt16(DDLPktype.SelectedItem.Value) Then dt.Rows.Add(dr) Else ClientScript.RegisterClientScriptBlock(Me.[GetType](), &#34;&#34;, &#34;alert(&#39;You can not insert more than &#34; &amp; dt.Rows.Count &amp; &#34; rows&#39;)&#34;, True) 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/rw4qMEU.gif" alt="" width="285" height="366" /></p>https://www.aspforums.net:443/Threads/207014/Set-ASPNet-GridView-row-add-limit-based-on-DropDownList-value-using-C-and-VBNet/https://www.aspforums.net:443/Threads/207014/Set-ASPNet-GridView-row-add-limit-based-on-DropDownList-value-using-C-and-VBNet/Wed, 21 Aug 2019 03:22:08 GMT