ASP.NET - how to get this ? - Asked By Jahir on 12-Mar-12 08:22 AM

this is my gridview output
if i edit 2nd row means the selected item state name want to selected in form dropdown1 and also city in dropdown2 how to write in edit event ?


ID State Name City Name Hotel Name Hotel Image Edit Delete
1 Tamilnadu Madurai tajt
2 Tamilnadu Madurai taj
3 Andrha kadappa renu


if (e.CommandName == "edt")

{


ImageButton img = (ImageButton)e.CommandSource as ImageButton;

GridViewRow row = img.NamingContainer as GridViewRow;

Label lid = (Label)row.FindControl("lblid");


///code... ?

}


<asp:TemplateField HeaderText="ID">

<ItemTemplate>

<asp:Label ID="lblid" runat="server" Text='<%#Eval("id") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="State Name">

<ItemTemplate>

<asp:Label ID="lblstatename" runat="server" Text='<%#Eval("statename") %>'></asp:Label>

</ItemTemplate>


</asp:TemplateField>

<asp:TemplateField HeaderText="City Name">

<ItemTemplate>

<asp:Label ID="lblcityname" runat="server" Text='<%#Eval("cityname") %>'></asp:Label>

</ItemTemplate>


</asp:TemplateField>

<asp:TemplateField HeaderText="Hotel Name">

<ItemTemplate>

<asp:Label ID="lblhotelname" runat="server" Text='<%#Eval("hotelname") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="Hotel Image">

<ItemTemplate>

<asp:ImageButton ID="lblfileupload" runat="server" ImageUrl='<%#Eval("path") %>' Width="100px" Height="100px" />

</ItemTemplate>

</asp:TemplateField>


<asp:TemplateField AccessibleHeaderText="Edit" HeaderText="Edit">

<ItemTemplate>

<asp:ImageButton ID="btnedit" runat="server" CommandName="edt" ImageUrl="~/Images/edit.png" />

</ItemTemplate>


<EditItemTemplate>

<asp:ImageButton ID="btnupdate" runat="server" Text="Update" OnClientClick="return confirm('Do You Want To Update')" CommandName="upd" ImageUrl="~/Images/update.png"></asp:ImageButton>

<asp:ImageButton ID="btncancel" runat="server" CommandName="can" Text="Cancel" ImageUrl="~/Images/cancel.png"></asp:ImageButton>

</EditItemTemplate>

</asp:TemplateField>


<asp:TemplateField AccessibleHeaderText="Delete" HeaderText="Delete">

<ItemTemplate>

<asp:ImageButton ID="btndelete" runat="server" CommandName="del" OnClientClick="return confirm('Do You Want To Delete?')" ImageUrl="~/Images/delete.png" />

</ItemTemplate>

</asp:TemplateField>

D Company replied to Jahir on 12-Mar-12 10:40 AM
Rahul,

First of all u have to put Dropdown control in edit item template , do something like this

 <EditItemTemplate>
 <asp:DropDownList runat="server" ID="myddl">
 </asp:DropDownList>
 </EditItemTemplate

Second thing

on EditCommand and find the dropdown list in the event handler like this

DropDownList ddl = (e.Item as GridDataIte)["Templatecolname"].FindControl("myddl") as DropDownList;

// here bind your grid
//make  a connection to sql
//fire a query, select from a particular column
//assign this to your dropdown list


a complete  example is given here

http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0

Let me know if u still face any issue
Regards
D
[)ia6l0 iii replied to Jahir on 12-Mar-12 01:38 PM
You are almost there. 

a) Step 1: Modify your Template Fields - State, City fields. Add EditeItemTemplates with a dropdownlist and Set the selected values of the dropdown. The chages are marked in bold, below.
<asp:TemplateField HeaderText="State Name">

<EditIemTemplate>
  <asp:DropDownList ID="ddStates" runat="server" 
                            DataTextField="
statename" DataValueField="stateId
                           
 SelectedValue='<%# Bind("statename") %>'
     DataSource='<% BindStates() %>'>
  
</asp:DropDownList>

<EditIemTemplate>
<
ItemTemplate>
      <asp:Label ID="lblstatename" runat="server" Text='<%#Eval("statename") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>

b) Step 2: Note the BindStates method for binding the states to the dropdown in the markup. This is what returns the states with the statename, and the stateid fields. 

c) Step 3: OR you can keep the dropdownlist clean, and populate it in the RowDatabound event as shown below. Following is an example that binds the dropdownlist of states.
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvEmployees.EditIndex == e.Row.RowIndex)
    {
        DropDownList ddlStates = (DropDownList)e.Row.FindControl("ddlStates");
        ddlStates.DataSource =         //Get the data
        ddlStates.DataTextField = "statename";
        ddlStates.DataValueField = "stateid";
        ddlStates.DataBind();
        ddlStates.Items.FindByValue((e.Row.FindControl("lblstatename") as Label).Text).Selected = true;
    }
}

d) Step 4: To update, write an event handler and hook to the OnRowUpdating event. You can make the changes in the markup as follows: <asp:GridView ID="gvEmployees" DataKeyNames = "hotelname" runat="server" OnRowUpdating = "UpdateHotel" OnRowDataBound = "RowDataBound"> e) Step 5: Capture the required and updated fields in the new event handler that you added. protected void UpdateHotel(object sender, GridViewUpdateEventArgs e)
{
        string state = (gvEmployees.Rows[e.RowIndex].FindControl("ddlStates") as DropDownList).SelectedItem.Value;
        string hotelId = gvEmployees.DataKeys[e.RowIndex].Value.ToString();
       //update with the details collected above
}

Needless to say, you need to follow similar stuff for Cities as well. Hope it is clear now.