Tuesday, May 1, 2012

Bind DropDownList in Gridview


Binding values in Dropdownlist in Gridview at runtime is not a big task.We can do it using RowDatabound event.In the given example dropdownlist is being populated at the time of editing.
At first declare a dropdownlist control in EditItemTemplate.

 <asp:TemplateField HeaderText="State" SortExpression="StateName" >
 <EditItemTemplate>
      <asp:DropDownList ID="ddlStateNm" runat="server" DataTextField="StateName"    DataValueField="StateID"> </asp:DropDownList>
      <asp:Label ID="lblStateID" runat="server" Text='<%# Bind("StateID") %>' Visible="false"></asp:Label>
 </EditItemTemplate>
 <ItemTemplate>
         <asp:Label ID="lblStateName" runat="server" Text='<%# Bind("StateName") %>'></asp:Label>
 </ItemTemplate>
</asp:TemplateField>

Write a Function to bind DropDownList.
 public void BindStateddl(DropDownList ddl)
    {
        SqlDataAdapter adp = new SqlDataAdapter("SELECT [StateID],[StateName] FROM [dbo].[MST_State] order by [StateName] asc", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);


        ddl.DataSource = ds;
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("---Select---", "0"));
    }

Now the most important part the RowDataBound event. Here at first check for DataRow so that it will overlook HeaderRow and then check for EditIndex so that Databinding to DropDownList will be implemented to editble row only.

   protected void gvCourt_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
         
            if (gvCourt.EditIndex == e.Row.RowIndex) //to overlook header row
            {
                string StateId = ((Label)e.Row.FindControl("lblStateID")).Text;


                DropDownList ddlGridState = (DropDownList)e.Row.FindControl("ddlStateNm");
           
                BindStateddl(ddlGridState);


                ddlGridState.SelectedValue = StateId;
            }
        }
    }


15 comments:

  1. hi anukana saha,
    this is bharath.

    i want to populate data in dropdownlist in asp gridview,from sql server data table only.

    ex: i am having table country,and i am inserting data to states table with using country name, and i want to populate country data in dropdownlist in aspgridview in states form....,

    ReplyDelete
  2. Bharath..above article is fulfilling ur purpose...only u have to write a proper filtered query

    ReplyDelete
  3. hi anukana saha,
    this is bharath.

    i want to populate data in dropdownlist in asp gridview,from sql server data table only.

    ex: i am having table Districts,and i am inserting data to Distances form with using of aspgridview in dropdownlist,in that case District table data binding to dropdownlist..
    could please help me...

    ReplyDelete
    Replies
    1. Bharath..do u get success to fetch the data in normal dropdown?Bocz above example is fetching data from sql server database. At first bind a simple dropdown a/c to ur query..then read this article again.

      Delete
    2. Hii Mam !! How I will Populate one dropdown list in value on selection of other dropdownlist value in GridView Without using Ajax.

      Delete
  4. drop down inside a grid should have default value as select when there is a null value from a table

    ReplyDelete
  5. hi anukana saha,
    this is bharath.

    i have a dubug in Asp.Gridview , could u help me.

    i am inserting data into gridview with using of templatefields, while i am inserting into the grid duplicate records how can i find ....

    ReplyDelete
    Replies
    1. Hello Bharath,
      Ur ques is not clear...duplicacy must be checked on some certain feild...and after finding duplicates what u want to do.U can do it in various way according to ur requirement.

      Delete
  6. YES I M AGREE WITH ANUKANA FROM JAY

    ReplyDelete
  7. hi anukana saha,
    this is bharath.


    how to get a month total dates in sql server 2008.
    i want a result of like this
    1-5-2013
    2-5-2013
    3-5-2013
    4-5-2013
    .
    .
    .
    .
    31-5-2013

    ReplyDelete
    Replies
    1. Your Solution is here -:
      http://dotnetknowledgebox.blogspot.in/2013/05/return-all-dates-of-selected-month.html

      Delete
  8. Hi ,this is sankar,

    after populating that drop down list data, how can i set the default selected selected value into drop down .

    like this,


    ReplyDelete
  9. Hello to everybody,
    My code raising an error for
    "DropDownList ddlUserEmailid = (DropDownList)e.Row.FindControl("ddlUserEmailid");"
    Error: Object reference not set to an instance of an object

    I also tried this:
    "DropDownList ddlUserEmailid = gridImport.FindControl("ddlUserEmailid") as DropDownList;"

    Please suggest.

    ReplyDelete
  10. The DataGridView .NET control supports a variety of data sources, such as any XML, custom business object and web service, including dropdown list

    ReplyDelete
  11. I am pradeep from Delhi i need one help.i select one dropdownlist value and get the result in Gridview but i select another value in dropdown means that values in same page.. please reply me..Thanks.

    ReplyDelete