Click here to Skip to main content
15,071,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to bind a dropdownlist from db in a gridview. But its not getting bind. My code is this but it is not working. I am just binding this on gridview row updating command.

C#
        SqlConnection con = new SqlConnection(constr);
        da = new SqlDataAdapter("sp_BindCourse", con);
        da.Fill(ds,"temp");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlCourse.DataSource = ds;
            ddlCourse.DataTextField = "course";
            ddlCourse.DataValueField = "course";
            ddlCourse.DataBind();
            ddlCourse.Items.Insert(0, "Course");
}


Please help me.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
[edit2]Comment changed to reflect new text on "Treat my content..." option - OriginalGriff[/edit2]
Posted
Updated 5-Aug-11 21:39pm
v3

Try using the code in GridView's DataBound event
   
C#
string connString =ConfigurationManager.

ConnectionStrings["connectionstring"].ToString()

SqlConnection con = new SqlConnection(connString);

con.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "spChGetNames";

cmd.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

da.Fill(ds);

DropDownList1.DataSource = ds;

DropDownList1.DataTextField = "Name";

DropDownList1.DataValueField = "ID";

DropDownList1.DataBind();
   
v3

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900