Click here to Skip to main content
14,389,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I want to use dropdownlist inside gridview but I am unable to do this.
I am new in asp.net and I have bind gridview but there dropdownshowing blank and other column filled.

How can I do that?

My gridview binds like:

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
           SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds, "product_detail");
           gridView1.DataSource = ds;
           gridView1.DataBind();


What is code use for dropdown list?
Above code filled product rate but I want select item from dropdownlist and then display rate for selected item.

aspx for gridview is

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" PageSize="10" AllowPaging="true">
                    <Columns>
                        <asp:TemplateField HeaderText="Product_Name">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="product_id" DataTextField="product_name" Width="100px" OnSelectedIndexChanged="SelectedindexChanged">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Product_Rate">
                            <ItemTemplate>
                                <asp:Label ID="lblRate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Product_Rate") %>'></asp:Label></ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
Posted
Updated 13-Oct-10 1:50am
v3
Comments
Bikash Shrestha From Nepal 13-Oct-10 2:44am
   
the way you are trying to do is not worthy.Because, you are trying to populate products in a gridview then whats the need of using dropdownlist inside gridview.

gridview is itself meant for listing collection of records.i mean to say no dropwown is needed.
m@dhu 13-Oct-10 2:51am
   
Tried to update question but unable to do so.
Dalek Dave 13-Oct-10 4:36am
   
Edited for Grammar and Code Blocks.
Rate this:
Please Sign up or sign in to vote.

Solution 4

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Products">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" DataTextField="Name" DataValueField="Price" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="--Select--" Value="">
                    </asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Price">
                <ItemTemplate>
                    <asp:Label ID="lblPrice" runat="server" Text="Label"></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           LoadGrid();
       }
   }

   private void LoadGrid()
   {
       try
       {
           DataTable dt = new DataTable();
           DataColumn dc1 = new DataColumn("Product Name");
           DataColumn dc2 = new DataColumn("Price");

           dt.Columns.Add(dc1);
           dt.Columns.Add(dc2);

           DataRow dr = dt.NewRow();
           dt.Rows.Add(dr);

           GridView1.DataSource = dt;
           GridView1.DataBind();

       }
       catch (Exception)
       {
           throw;
       }

   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           DataTable products = (DataTable)GetProducts();//GetProducts() is the Method to Get Products list
           DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
           ddl.DataSource = products;
           ddl.DataBind();

           e.Row.Cells[1].Text = "";
       }



   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList ddl = (DropDownList)GridView1.Rows[0].FindControl("DropDownList1");
       GridView1.Rows[0].Cells[1].Text = ddl.SelectedValue;
   }
   
v2
Comments
shanjeev 13-Oct-10 3:42am
   
great job.it works good.
Dalek Dave 13-Oct-10 4:37am
   
Good Answer
call to .net 14-Oct-10 1:34am
   
how can i create getproducts method??
Thanks
Bikash Shrestha From Nepal 15-Oct-10 15:47pm
   
you can create getproducts methods as shown bwlow

DataTable getproducts()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "product_detail");
return ds.Tables[0];
}
Bikash Shrestha From Nepal 15-Oct-10 15:51pm
   
hello Shivesh you can create getproducts methods as shown bwlow

private DataTable getproducts()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "product_detail");
return ds.Tables[0];
}
sweeneel 21-Oct-10 0:34am
   
Thanx a lot .........
Rate this:
Please Sign up or sign in to vote.

Solution 2

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           //Handle the Gridview RowDatabound Event
           if(e.Row.RowType  == DataControlRowType.DataRow)
           { //Find the DropDownList inside the Rows
               DropDownList ddl=(DropDownList)e.Row.FindControl("DropDownList1");
               //Bind the Temporory Table we have Created
               ddl.DataSource=//datasource;
               //Set the Display value
               ddl.DataTextField="City";
               //Bind the values to DropDownList
               ddl.DataBind();

           }
       }
   
Comments
call to .net 13-Oct-10 2:58am
   
thanks a lot...
Dalek Dave 13-Oct-10 4:37am
   
Good Call
Rate this:
Please Sign up or sign in to vote.

Solution 5

hello Shivesh you can create getproducts methods as shown bwlow

private DataTable getproducts()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "product_detail");
            return ds.Tables[0];
}

:-O :-O
   
Comments
Jipin 21-Oct-10 3:08am
   
thanks boka it helped me too
Rate this:
Please Sign up or sign in to vote.

Solution 1

<asp:dropdownlist id="DropDownList1" runat="server" datavaluefield="product_id" datatextfield="product_name" width="100px" onselectedindexchanged="SelectedindexChanged" xmlns:asp="#unknown" />


you need to bind the DataSource of your dropdownlist.

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                  SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataSet ds = new DataSet();
                  da.Fill(ds, "product_detail");
                  gridView1.DataSource = ds;
                  gridView1.DataBind();

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType  == DataControlRowType.DataRow)
            {   
                  DropDownList DropDownList1 =  (DropDownList)e.Row.FindControl("DropDownList1");
                  SqlConnection con = new    SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                  SqlCommand cmd = new SqlCommand("select  product_name,product_rate from product_detail", con);
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataSet ds = new DataSet();
                  da.Fill(ds, "product_detail");

                  //Bind the DataSource to Dropdown list
                  DropDownList1.DataSource = ds.Table[0];
                  DropDownList1.DataTextField="product_name";
                  DropDownList1.DataValueField ="product_id";
                  DropDownList1.DataBind();
            }
        }


If this helped you then please Vote and mark it as answer.
   
v2
Comments
call to .net 13-Oct-10 2:43am
   
where i have to bind dropdownlist above code i have written on page load.
when i have try to bind dropdownlist on pageload the intelligence not showing dropdownlist1
NMehta83 13-Oct-10 2:59am
   
I update the answer.
Rate this:
Please Sign up or sign in to vote.

Solution 3

//You need DataBound event of the Grid View to bind Dropdown list
//First You have to put the DataSet in ViewState to be accessed in
//Row Bound Event

//So the Code will be like follows
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                  SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_<code><code></code></code>detail", con);
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataSet ds = new DataSet();
                  da.Fill(ds, "product_detail");
                  ViewState["ProductDetail"] = ds;
                  gridView1.DataSource = ds;
                  gridView1.DataBind();


//RowBound Event of Grid view
<pre>protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {

DropDownList drp = null;
GridViewRow grow = e.Row;
drp = (DropDownList) grow.findControl("DropDownList1");
if(drp!=null)
{
DataSet ds =(DataSet) ViewState["ProductDetail"];
 drp.DataSource = ds;
drp.DataBind();
}
                }
catch(Exception ex)
{}


}
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100