Skip to content
Loading
How to add Data in gridview from Dropdownlist ?
  1. private SqlConnection con = new SqlConnection(@"");  
  2.        private DataTable dt = new DataTable();  
  3.        private DataRow dr;  
  4.        protected void Page_Load(object sender, EventArgs e)  
  5.        {  
  6.            if (!this.IsPostBack)  
  7.            {  
  8.                dt.Columns.Add("Codeitem");  
  9.                dt.Columns.Add("Descriptionitem");    
  10.                dt.Columns.Add("QTY");  
  11.                ViewState["dt"] = dt;  
  12.                itemload();  
  13.            }  
  14.        }  
  15.   
  16.        private void itemload()  
  17.        {  
  18.            con.Open();  
  19.            SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);  
  20.            DataSet dspr1 = new DataSet();  
  21.            adpr1.Fill(dspr1);  
  22.            DropDownList1.DataSource = dspr1.Tables[0];  
  23.            DropDownList1.DataTextField = "Descriptionitem";  
  24.            DropDownList1.DataValueField = "Codeitem";  
  25.            DropDownList1.DataBind();  
  26.        }  
  27.   
  28.        protected void GVadd_Click(object sender, EventArgs e)  
  29.        {  
  30.            dt = ViewState["dt"as DataTable;  
  31.            dr = dt.NewRow();  
  32.            dr["Codeitem"] = DropDownList1.SelectedValue;  
  33.            dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();  
  34.   
  35.            dr["QTY"] = txtqty.Text;  
  36.            dt.Rows.Add(dr);  
  37.            GridView1.DataSource = dt;  
  38.            GridView1.DataBind();  
  39.            clear();  
  40.        }  
  41.   
  42.        private void clear()  
  43.        {  
  44.            // Codeitem.Text = "";    
  45.            txtqty.Text = "";  
  46.        }  
Output will be as below
 
  • Akhter HUssain
    Hi Puneet kankar,
     
    my dropdownlist is not in gridview ,it is out of gridview ,i want to insert data from dropdownlist to  gridview...
     
    i have provided html below...and i want to insert textfiled and  valuefield into gridview.
  • Puneet Kankar
    Hi Akhter,
     
    Go through given below link, You can use ViewState and session for hold data in grid view and button click event you can write code for put dropdown data in gridview 
     
    https://forums.asp.net/t/1915866.aspx?How+to+add+Data+To+GridView+From+TextBox+DropDownList
  • Akhter HUssain
    No i do not want this....
    i want to add data from dropdownlist into gridview here is  
     
    html 
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>  
    2.   
    3. >  
    4.   
    5. <html xmlns="http://www.w3.org/1999/xhtml">  
    6. <head runat="server">  
    7.     <title>title>  
    8. head>  
    9. <body>  
    10.     <form id="form1" runat="server">  
    11.     <div>  
    12.         CodeItems :  
    13.         <asp:DropDownList ID="DropDownList1" runat="server">  
    14.         asp:DropDownList>  
    15.   
    16. <br />  
    17. Qty :  
    18. <asp:TextBox runat="server" ID="txtqty" />  
    19. <br />  
    20.         <asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />  
    21.   
    22.     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">  
    23.     <Columns>  
    24.         <asp:BoundField DataField="Codeitem" HeaderText="Code" />  
    25.          <asp:BoundField DataField="Descriptionitem" HeaderText="Name" />  
    26.         <asp:BoundField DataField="QTY" HeaderText="QTY" />  
    27.     Columns>  
    28. asp:GridView>  
    29.           
    30. <br />  
    31.   
    32.     div>  
    33.     form>  
    34. body>  
    35. html>  
     
     here is my c# Code
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Data;  
    8. using System.Data.SqlClient;  
    9.   
    10. namespace WebApplication1  
    11. {  
    12.     public partial class WebForm2 : System.Web.UI.Page  
    13.     {  
    14.         SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;");  
    15.   
    16.         DataTable dt = new DataTable();  
    17.         DataRow dr;  
    18.         protected void Page_Load(object sender, EventArgs e)  
    19.         {  
    20.             if (!this.IsPostBack)  
    21.             {  
    22.                 dt.Columns.Add("Codeitem");  
    23.                // dt.Columns.Add("Descriptionitem");  
    24.                 dt.Columns.Add("QTY");  
    25.                 ViewState["dt"] = dt;  
    26.                 itemload();  
    27.             }  
    28.         }  
    29.   
    30.         private void itemload()  
    31.         {  
    32.             con.Open();  
    33.             SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);  
    34.             DataSet dspr1 = new DataSet();  
    35.             adpr1.Fill(dspr1);  
    36.             DropDownList1.DataSource = dspr1.Tables[0];  
    37.             DropDownList1.DataTextField = "Descriptionitem";  
    38.             DropDownList1.DataValueField = "Codeitem";  
    39.             DropDownList1.DataBind();  
    40.         }  
    41.   
    42.         protected void GVadd_Click(object sender, EventArgs e)  
    43.         {  
    44.             dt = ViewState["dt"as DataTable;  
    45.             dr = dt.NewRow();  
    46.             dr["Codeitem"] = DropDownList1.SelectedValue;  
    47.            // dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();  
    48.   
    49.             dr["QTY"] = txtqty.Text;  
    50.             dt.Rows.Add(dr);  
    51.             GridView1.DataSource = dt;  
    52.             GridView1.DataBind();  
    53.             clear();  
    54.         }  
    55.   
    56.         private void clear()  
    57.         {  
    58.            // Codeitem.Text = "";  
    59.             txtqty.Text = "";  
    60.         }  
    61.     }  
    62. }  
     i want both filed to display in gridview Description item and codeitem
    1. DropDownList1.DataTextField = "Descriptionitem";
    2. DropDownList1.DataValueField = "Codeitem";
     
  • Priyanka Jain
    You need to use onRowDataBound event of GridView to bind DropdownList in Gridview 
     
    For more detail you can refer below link
    https://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx