Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
<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:
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 11-Nov-11 3:17am
Edited 11-Nov-11 3:37am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I usually do inline binding for such nested ddls inside GridView/ListView.
<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>
  Permalink  
Comments
ganesan2510 at 13-Nov-11 12:58pm
   
i need to bind only in code behind
thanks for ur reply..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{}
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You need fill dropdownlist with data source. use RowDataBound event
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

how to set datasource to dropdownlist in gridview at page load from another table in asp.net c#
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

you can do this in code behind only after rowcreated, so you cannot do this in page load.
  Permalink  
Comments
ganesan2510 at 13-Nov-11 12:56pm
   
how to call dropdownlist in code behind after row created?
please explain little more..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

at last i got the answer and thanks for ur consideration.. Smile | :)
 
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)
             {
                             }
 
         }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 185
2 jlopez788 144
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,559
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100