1

im working on an asp.net website with a gridview.

The gridview has data from an sql database.

Like:

Cuntry----Name
USA--------John
England----Frank

...

The data is loaded in to the gridview like this:

SqlCommand cmd;
        cmd = new SqlCommand();
        cmd.Connection = sqlConn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_loadData";
        sqlConn.Open();

        dr = cmd.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

So, in the name column, i want a dropdownlist. And I want the dropdownlist with the corresponding value from the database selected.

How can I do this?

Thanks

1 Answer 1

1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
  OnRowDataBound="GridView1_RowDataBound">    
  <Columns>
  <asp:TemplateField HeaderText="Country">
    <ItemTemplate>
      <asp:DropDownList Width="50" runat="server" id="ddlCountry" AutoPostBack="true">
      </asp:DropDownList> 
    </ItemTemplate>
  </asp:TemplateField>
</asp:GridView>

In Code-behind

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //Checking whether the Row is Data Row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Finding the Dropdown control.
        Control ctrl = e.Row.FindControl("ddlCountry");
        if (ctrl != null)
        {
            DropDownList dd = ctrl as DropDownList;
            //Here Bind with country list
            dd.DataSource = lst;
            //Put here the object properties name
            dd.DataValueField = "idCountry";
            dd.DataTextField = "nameCountry";
            dd.DataBind();
            //Here add the code to select the current user country in the list
            int idUserCountry = 10;
            dd.Items.FindByValue(idUserCountry).Selected = true;

        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

im doing this: code cmd = new SqlCommand(); cmd.Connection = sqlConn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_Select"; sqlConn.Open(); dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); dd.DataSource = dt; dd.DataBind(); .... but when i execute, in the dropdownlist, all i get is: "System.Data.DataRowView". No actual values are shown. How can I fix this?
inside the protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
this part is not working: code //Here add the code to select the current user country in the list int idUserCountry = 10; dd.Items.FindByValue(idUserCountry).Selected = true; .............it gives me a nullreference exception..........what can I do?
This is only an example if you have null reference exception it means you dont have a country with id=10. I Cant give you the whole code, you have to adapt to your problem, you got here 99% of the job done, so make the last effort, for example you can use the debug mode to find out. Come on, understanding and copy pasting is not the same thing :)
You are right. I did the effort. Its working good now. Thanks for all the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.