Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I have a gridview.In the gridview there is a priority column.In the <edititemtemplate> of the priority I am using dropdown list.Suppose if i select edit button it is not showing the selected drop down values.Plz help me.

Thanks in advance.
Posted

You need to set the EditIndex.
Have a look at this.
This might help u
Populate-DropDownList-with-Selected-Value-in-EditItemTemplate-of-GridView-in-ASPNet.aspx[^]
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 3-Jan-14 2:09am    
5, good link..
Naz_Firdouse 3-Jan-14 2:12am    
Thanks Karthik
add drop down list to edittemplate as following
C#
<asp:Gridview AutoGenerateColumns="false" ID="gv" OnRowEditing="Row_Edit" ...................>
<Columns>
 ------------------------
 ------------------------
 <asp:TemplateField>
  <ItemTemplate>
  // add label to show data
  </ItemTemplate>
  <EditTemplate>
    <asp:DropdownList ID="ddl" runat="server">
    <asp:ListItem Text="Select" Value="0" />
    </asp:DropdownList>
   </EditTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
 
Share this answer
 
v2
Comments
Member 10234093 3-Jan-14 2:04am    
I have already added drop down list in edititem template.If we click edit link button drop down is not displaying the selected value.
Vinay Jade 3-Jan-14 2:08am    
in editrow event take out value from database which you want to bind to dropdownlist then use
ddl.Items.FindByText(retrived value from database).Selected=true;
Plz try below code it's really helping to you becz i already made it in my application....

code on .aspx page
---------------------------------------------
XML
<asp:TemplateField HeaderText="Location">
                   <ItemTemplate>
                       <asp:Label ID="lbllocation" runat="server" Text='<%#Bind("location") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:Label ID="lblloc" runat="server" Text='<%#Bind("location") %>' Visible="false"></asp:Label>
                       <asp:DropDownList ID="drploc" runat="server">
                       </asp:DropDownList>
                   </EditItemTemplate>
               </asp:TemplateField>


----------------------------------------------

Code on .aspx.cs page
----------------------------------------------
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                //Code for binding dropdown on selected index of edit

                Label lblloc = (Label)e.Row.FindControl("lblloc");
                DropDownList drploc = (DropDownList)e.Row.FindControl("drploc");

                DataSet ds = new DataSet();
                ds = rb.Fill_Location();

                if (ds.Tables[0].Rows.Count != 0)
                {
                    drploc.DataSource = ds.Tables[0];
                    drploc.DataTextField = "location";
                    drploc.DataValueField = "location_id";
                    drploc.DataBind();
                }
                drploc.Items.FindByText(lblloc.Text).Selected = true;              
                            
            }
        }
    }
 
Share this answer
 
v2
Comments
Naz_Firdouse 3-Jan-14 4:12am    
added tags to code behind logic
Member 14722303 21-Jan-20 5:50am    
kaam nahi kar raha hai code
Member 10234093 3-Jan-14 4:25am    
thanks
Chintan Desai1988 6-Jan-14 3:19am    
u welcome dear..... :-)
Aashish68 13-Mar-17 8:19am    
Its working like a Charm!!! ...Thanks

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