Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I have an gridview with 5 columns with that 4 textbox and One Dropdownlist.

Now i want to load my dropdownlist through an datasource or datatable.

How to do that? Even i need to load my footer also...Please help me out

I tried...My code is below

My .aspx code
XML
<asp:TemplateField HeaderText="Products">
                  <EditItemTemplate>
                  <asp:DropDownList ID="txt_grd_ProdName" runat="server" Width="95px" AutoPostBack="True" onselectedindexchanged="txt_grd_ProdName_SelectedIndexChanged" ><asp:ListItem>-Select-</asp:ListItem> </asp:DropDownList>
                  </EditItemTemplate>
                 <ItemTemplate>
                         <%#Eval("Prod_name") %>
                  </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <FooterTemplate>
                    <asp:DropDownList ID="Foot_Txt2" runat="server" Width="95px" AutoPostBack="True"><asp:ListItem>-Select-</asp:ListItem> </asp:DropDownList>
                </FooterTemplate>
                  </asp:TemplateField>

My aspx.cs Code
protected void Grid_Purchase_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
                DropDownList dropdown = (DropDownList)e.Row.Cells[1].FindControl("txt_grd_ProdName");
                DataTable dt = new DataTable();
                dt = SQLobj.SQL_Adapter("Select Prod_name from ProductDetails");
                dropdown.DataSource = dt;
                dropdown.DataBind();
           
        }
}
Posted
Updated 4-Jul-11 20:48pm
v2

 
Share this answer
 
Comments
[no name] 5-Jul-11 3:27am    
Good Link. My 5 too.
J.Karthick 5-Jul-11 3:33am    
But here i need to add an sqldatasource....but i want to add dynamically when the page is loading or something else....
try this

protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (grdDevelopment.EditIndex == e.Row.RowIndex && e.Row.RowType==DataControlRowType.DataRow)
   {
       DropDownList drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("Your DropDownName");
   }
}
 
Share this answer
 
Comments
[no name] 5-Jul-11 3:37am    
Good Call. My 5 too
Mahendra.p25 5-Jul-11 3:38am    
Thanks
Hello,

you can bind the data source on row data bound event of grid.... as i can see you are just binding the source define datatext and datavalue columns as well.

try it out

thanks
sanjeev.
 
Share this answer
 
v2
EOD... i found the solution....

I should assume column name to bind drdList.DataTextField = "Prod_name";

My code is...

ONPAGE_LOAD()
{
.....
Grid_Purchase.DataSource = dt; //dt contains empty rows
            Grid_Purchase.DataBind();
            DataTable myDatatable1 = new DataTable();
            myDatatable1 =  SQLobj.SQL_Adapter("select Prod_name from ProductDetails");
            DropDownList drdList;
            DataRow dr;
            dr = myDatatable1.NewRow();
            dr["Prod_name"] = "-Select-";
            myDatatable1.Rows.InsertAt(dr, 0);
            foreach (GridViewRow grdRow in Grid_Purchase.Rows)
            {
                drdList = (DropDownList)(Grid_Purchase.Rows[grdRow.RowIndex].Cells[1].FindControl("txt_grd_ProdName"));
                drdList.DataSource = myDatatable1;
                drdList.DataTextField = "Prod_name";   
                drdList.DataBind();
            }
            
}



My aspx ,page

XML
<asp:TemplateField HeaderText="Products">
                 <ItemTemplate>
                       <asp:DropDownList ID="txt_grd_ProdName" runat="server" Width="95px" AutoPostBack="True" onselectedindexchanged="txt_grd_ProdName_SelectedIndexChanged"><asp:ListItem>-Select-</asp:ListItem> </asp:DropDownList>
                  </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <FooterTemplate>
                    <asp:DropDownList ID="Foot_Txt2" runat="server" Width="95px" AutoPostBack="True" ></asp:DropDownList>
                </FooterTemplate>
                  </asp:TemplateField>
 
Share this answer
 
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