Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi developers,
my problem is how to access the dropdownlist which is under Grid view?

example: when ever i select a class all students who are in that class should be come, like:
name     section<br />
stephen     A
Roy         B
Nani        A
Thiru       C


like wise i have to display, here section is drop down list .

i used like following code,

string sections = String.Empty;        
DataTable dt = new DataTable();        
dt.Columns.Add("sectionid");        
foreach (GridViewRow row in gvsectionchange.Rows)        
{
    sections = ((DropDownList)row.FindControl("ddlChangeSection")).SelectedItem.Value;
    DataRow dr = dt.NewRow();        
    dr["sectionid"] = sections;        
    dt.Rows.Add(dr);
}


here is some problem,drop down is filling but not displaying the exact sections.

Thank u,
Best Regards,
Thiru
Posted
Updated 13-Apr-10 8:44am
v3

1 solution

Looks like you have copied piece of code from here.[^]and just tried it. What you are trying is different than what's being done in the sample of that link.

You don't need to make the datatable in order to fill the dropdown and display the appropriate sections in dropdownlist.

Use a template field, where in ItemTemplate fill/set Dropdown values.
One of the ways to do it can be:
XML
<asp:TemplateField HeaderText="User Name">
  <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" DataSourceID="SqlDataSource1" DataValueField=sectionname AutoPostBack="True" SelectedValue='<%# Bind("sectionname")%>'>
       <asp:ListItem Value="">Select an Item</asp:ListItem>
    </asp:DropDownList>
  </ItemTemplate>
</asp:TemplateField> 


If still needed, just google, how to add dropdown to gridview, and you will find lots of writeup on that. :thumbsup:
 
Share this answer
 

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