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.Save DropDownList selected Value in database and display selected Text 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;GVadd_Click&#34; /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#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;/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) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dt.Columns.Add("Descriptionitem"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dt.Columns.Add("Qty"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dtddl = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtddl.Columns.Add("Descriptionitem"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtddl.Columns.Add("Codeitem"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtddl.Rows.Add("A", 1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtddl.Rows.Add("B", 2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtddl.Rows.Add("C", 3); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddlItems.DataSource = dtddl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddlItems.DataTextField = "Descriptionitem"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddlItems.DataValueField = "Codeitem"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddlItems.DataBind(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddlItems.Items.Insert(0, new ListItem("Select", "0")); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewState["dt"] = dt; &nbsp;&nbsp;&nbsp; } } protected void GVadd_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; dt = ViewState["dt"] as DataTable; &nbsp;&nbsp;&nbsp; dr = dt.NewRow(); &nbsp;&nbsp;&nbsp; dr["Descriptionitem"] = ddlItems.SelectedItem.Text; &nbsp;&nbsp;&nbsp; dr["QTY"] = txtqty.Text; &nbsp;&nbsp;&nbsp; dt.Rows.Add(dr); &nbsp;&nbsp;&nbsp; GridView1.DataSource = dt; &nbsp;&nbsp;&nbsp; GridView1.DataBind(); }</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 GVadd_Click(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 dt.Rows.Add(dr) GridView1.DataSource = dt GridView1.DataBind() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/FjyaCgf.gif" alt="" width="251" height="266" /></p>https://www.aspforums.net:443/Threads/100431/Save-DropDownList-selected-Value-in-database-and-display-selected-Text-in-ASPNet-GridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/100431/Save-DropDownList-selected-Value-in-database-and-display-selected-Text-in-ASPNet-GridView-using-C-and-VBNet/Tue, 06 Aug 2019 06:22:01 GMT