Click here to Skip to main content
15,041,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a gridview1 which should contain a dropdown in its column while editing... can u plz help me out... thnx...


XML
<asp:TemplateField HeaderText="Project">
       <EditItemTemplate>
      <asp:DropDownList ID="ddlProjects" runat="server" DataSourceID="DataSource1" DataTextField="" DataValueField="" ></asp:DropDownList>
       </EditItemTemplate>
       <ItemTemplate>
       <asp:Label ID="lblProjectName" runat="server" Text='<%# Eval("Project_Name") %>'></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>
Posted
Updated 28-Sep-12 19:35pm
v2

hi,

In itemDataBound Event, use the below code,

C#
if(e.Row.RowType == DataControlRowType.DataRow)
{
   if(gridview1.EditIndex >= 0)
   {
      DropDownList ddl = (DropDownList)e.Row.FindControl("ddlProjects");
      ddl.DataTextField = "Col1";
      ddl.DataValueField = "COl2";
      ddl.DataSource = objDatatable; //your datasource
      ddl.DataBind();
   }
}


hope it helps,
   
v3
// U should try like this :

XML
<asp:TemplateField HeaderText="Project">
       <EditItemTemplate>
      <asp:DropDownList ID="ddlProjects" runat="server"    ></asp:DropDownList>
       </EditItemTemplate>
       <ItemTemplate>
       <asp:Label ID="lblProjectName" runat="server" Text='<%# Eval("Project_Name") %>'></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>



// Write like that in .cs code 


 protected void gvCheck_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCheck.EditIndex = e.NewEditIndex;
        gvCheck.DataSource = ViewState["DataTable"] as DataTable;
        gvCheck.DataBind();
        DropDownList ddlTest = (DropDownList)gvCheck.Rows[e.NewEditIndex].FindControl("ddlTest");
        ddlTest.DataSource = ViewState["ddlTable"] as DataTable;
        ddlTest.DataTextField = "Name";
        ddlTest.DataValueField = "Name";
        ddlTest.DataBind();
        ddlTest.Items.Insert(0, new ListItem("Select", "0"));
    }
   

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