views:

706

answers:

2

hi to all

how to add a dropdownlist to gridview and how to add a datasource to dropdown.

for example:

i have gridview with 5 colomns c1,c2,c3,c4,c5 here i want to add a dropdownlist only c1 and c2 what is the procedure for do this work

note: vs2008,asp.net with c#

thanks

A: 

Check out the RowDataBound event of the GridView control.

There you can drop a new DropDownList into whatever cell you want to and bind it to any DataSource you like.

hunter
A: 

You can edit the columns in the GridView and set those two to be TemplateColumns, then put a DropDownList called DropDownList1 in the TemplateColumn. Then as Hunter says use the DataBound event to bind to say a dataset like so:

DropDownList list = e.Item.FindControl("DropDownList1");
list.DataSource = < your DataSet here>;
list.DataValueField = "code";
list.DataTextField = "description";
list.DataBind();
MikeW
thanks a lot mr.MikeW
thiru
i am writing code like this by your instructionserror will occur pls helperror is "Object reference not set to an instance of an object"in the line of --list.DataSource = ds;-- DataSet ds = new DataSet(); SqlCommand cmp = new SqlCommand("SELECT * from Projectcodetable ", conn); SqlDataAdapter dr = new SqlDataAdapter(cmp); dr.Fill(ds);DropDownList list = DropDownList)e.Row.FindControl("DropDownList1");list.DataSource = ds;list.DataValueField = "ProjectCode";list.DataTextField = "ProjectCode";list.DataBind();conn.Close();
thiru
thanks a lot mr.MikeW
thiru
My guess is it hasn't found the control DropDownList1. I'd debug through your code and see where it is failing. Probably "list" is null.Did you add the template column? Without doing the above code and populating the dropdownlists, if you run the page do the dropdowns display?
MikeW