Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am have a grid with 2 column Sno and Deptname
and in the deptname column i am having dropdownlist.
how to give datasource to that dropdownlist in Page_Load

i tried like this but it gives error

source code:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
           <Columns>
               <asp:TemplateField HeaderText="Sno">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text='<%# Bind("Sno") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="DeptName" >
                   <HeaderTemplate >
                   <asp:DropDownList ID="dd"  runat="server" AutoPostBack="true"
                           DataTextField="DeptName" AppendDataBoundItems="true"
                           onselectedindexchanged="dd_SelectedIndexChanged" >
                   <asp:ListItem>Select</asp:ListItem>
                   </asp:DropDownList>

                   </HeaderTemplate>
                   <ItemTemplate >
                       <asp:Label ID="Label1" runat="server" Text='<%# Bind("DeptName") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>



cs code:
C#
protected void Page_Load(object sender, EventArgs e)
   {
            con = new SqlConnection(@"Data Source=PC026328\SQLEXPRESS;Initial Catalog=gan;Integrated Security=True");
          SqlDataAdapter da = new SqlDataAdapter("select deptname from department", con);
          DataSet ds = new DataSet();
          da.Fill(ds);
//error line
<dropdownlist ddl="(DropDownList)gridView1.HeaderRow.FindControl("dd");">
ddl.DataSource = ds.Tables[0];
 }</dropdownlist>

help me..!
Posted
Updated 11-Nov-11 3:37am
v3

I usually do inline binding for such nested ddls inside GridView/ListView.
XML
<asp:GridView ID="gv1" runat="server" GridLines="None">
<Columns>
.....
<asp:TemplateField>
<li>
    <asp:DropDownList ID="ddl1" runat="server" SelectedValue='<%#Bind("mg1") %>' DataTextField="p_option" DataValueField="p_right_id" DataSourceID="SqlDataSource1">
    </asp:DropDownList>
</li>
</asp:TemplateField>
....
....
</GridView>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
        SelectCommand="My_SP" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>
 
Share this answer
 
Comments
ganesan2510 13-Nov-11 12:58pm    
i need to bind only in code behind
thanks for ur reply..
How can you give the data to DropDown list as its not yet created,
The DropDownList inside the GridView will be created after the Event RowCreated fires,as RowCreated Event fires after the Page_Load event,you cant do this in Page_Load(),
You have to do this in
C#
public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{}
 
Share this answer
 
v4
Comments
Member 10850583 28-Aug-14 17:24pm    
What if I am calling a function on page load which needs to reference something inside but it's not created until after rowcreated function? What do I do than?
at last i got the answer and thanks for ur consideration.. :)

C#
protected void MPCproject_RowDataBound(object sender, GridViewRowEventArgs e)
         {
             try
             {

                 if (e.Row.RowType == DataControlRowType.Header)
                 {
                     DropDownList ddl = (DropDownList)e.Row.FindControl("ddlSorting");
                     SqlDataAdapter da = new SqlDataAdapter("select deptname from department", con);
                     DataSet ds = new DataSet();
                     da.Fill(ds);
                     ddl.DataSource = ds.Tables[0];
                     ddl.DataTextField = "Projectype";
                     ddl.DataValueField = "Projectype";
                     ddl.DataBind();
                 }
             }
             catch (Exception ex)
             {
                             }

         }
 
Share this answer
 
You need fill dropdownlist with data source. use RowDataBound event

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
         {
                 if (e.Row.RowType == DataControlRowType.Header)
                 {
                     DropDownList dropDownLst = (DropDownList)e.Row.FindControl("dd");
                     SqlDataAdapter da = new SqlDataAdapter("select deptname from department", con);
                     DataSet objDS = new DataSet();
                     da.Fill(objDS);
                     dropDownLst.DataSource = objDS .Tables[0];
                     dropDownLst.DataTextField = "deptname";
                     dropDownLst.DataValueField = "deptname";
                     dropDownLst.DataBind();
                 };
        }


Remove DataTextField, AppendDataBoundItems, onSelectedIndexChanged from ASPX page
 
Share this answer
 
you can do this in code behind only after rowcreated, so you cannot do this in page load.
 
Share this answer
 
Comments
ganesan2510 13-Nov-11 12:56pm    
how to call dropdownlist in code behind after row created?
please explain little more..
how to set datasource to dropdownlist in gridview at page load from another table in asp.net c#
 
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