Click here to Skip to main content
16,015,756 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI ,

i am having problem in binding my dropdown in edit template ....i have tried many and can't do it.plz help .....
ASP.NET
<asp:TemplateField HeaderText="State">
              <ItemTemplate>
               <asp:Label ID="Label2" runat="server" Text='<%# Eval("State")%>'/>
             </ItemTemplate>
              <EditItemTemplate>
              <asp:Label ID="Label2" runat="server" Text='<%# Eval("State")%>' Visible = "false"></asp:Label>
             <asp:DropDownList ID="ddlStates"   runat="server"  Width="70px" ></asp:DropDownList>
              </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlState" runat="server"  Width="70px"  ></asp:DropDownList>
              </FooterTemplate>
          </asp:TemplateField>



C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
 if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
              {
                  DropDownList ddlState = (DropDownList)e.Row.FindControl("ddlState");
                  string query = "SELECT StateID,StateName from STCState";
                  SqlCommand cmd = new SqlCommand(query);
                  ddlState.DataSource = GetAllStates(cmd);
                  //ddlState.DataTextField = "StateName";
                  //ddlState.DataValueField = "StateID";
                  ddlState.DataBind();
                  ddlState.Items.FindByValue((e.Row.FindControl("Label2") as Label).Text).Selected = true;
              }
}

My IF CONDITION ALWAYS GET FALSE IT DOES NOT GO IN TO IF CONDITION WHEN I CLICK ON EDIT.......
C#
public DataTable GetAllStates(SqlCommand cmd)
     {
         SqlConnection conn = new SqlConnection();
         conn.ConnectionString = "Data Source=JEEVAN01\\SQLEXPRESS; Initial catalog =suresh; Integrated Security=true";
         conn.Open();
         using (SqlDataAdapter sda = new SqlDataAdapter())
         {
             cmd.Connection = conn;
             sda.SelectCommand = cmd;
             using ( DataTable DT=new DataTable())
             {
                 sda.Fill(DT);
                 return DT;
             }
         }
     }

Thanks in Advance
suresh
Posted
Comments
Prasad_Kulkarni 3-Aug-12 7:41am    
Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
Using all capitals means SHOUTING on web, and using all small letters means childish. Always use proper capitalization.
Remove shouting.

1 solution

Quite easy. You're doing it wrong, cause by that Event the control is not there.

F#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && 
        e.Row.RowState == DataControlRowState.Edit)
    { 
        // Here you will get the Control you need like:
        DropDownList dl = (DropDownList)e.Row.FindControl("ddlState");
        // Bind this DropDownList here..
    }
}




--Amit
 
Share this answer
 
v4
Comments
shuresh 3-Aug-12 8:16am    
& -> This can be apply.......
This condition also get Fails
_Amy 3-Aug-12 8:34am    
Try my updated 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