This is a migrated thread and some comments may be shown as answers.

Sropdown selected value in edit mode (Radgrid)

6 Answers 297 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jagat
Top achievements
Rank 1
Jagat asked on 28 Oct 2011, 07:55 PM

Hi,

I want to use dropdowns in a radgrid.

When i enter into edit mode, i want to show the selected value in the dropdown .

How can i do this?
So far this is my code.

Thanks




<telerik:GridTemplateColumn HeaderText="City">
                        <ItemTemplate>
                            <asp:Label ID="lblcity" runat="server" Text='<%# Bind("CityID") %>'></asp:Label>
                          
                              
                        </ItemTemplate>
                        <EditItemTemplate>
                          
                            <asp:DropDownList ID="ddlCity" runat="server">
                            </asp:DropDownList>
                          
                          
                        </EditItemTemplate>
                          
                          
                        </telerik:GridTemplateColumn>




Code Behind:

protected void gdv_Drivers_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
       {
            
           if ((e.Item is GridEditableItem && ((GridEditableItem)e.Item).IsInEditMode))
           {
               string where = null;
             //  string cityid = ((DropDownList)e.Item.FindControl("ddlcity")).SelectedValue;
             //  string stateid = ((DropDownList)e.Item.FindControl("ddlstate")).SelectedValue;
             //  string zipid = ((DropDownList)e.Item.FindControl("ddlzip")).SelectedValue;
               DropDownList ddlcity, ddlstate, ddlzip;
               ddlcity = ((DropDownList)e.Item.FindControl("ddlCity"));
               ddlstate = ((DropDownList)e.Item.FindControl("ddlState"));
               ddlzip = ((DropDownList)e.Item.FindControl("ddlZip"));
               myGlobal.LoadDropDown(ddlcity, "tbl_CityLkup", "CityID", "CityNm", "GCRPC", "CityID", "CityNm", where);
               myGlobal.LoadDropDown(ddlstate, "tbl_StateLkup", "StateID", "StateNm", "GCRPC", "StateID", "StateNm", where);
               myGlobal.LoadDropDown(ddlzip, "tbl_ZipLkup", "ZipID", "ZipCd", "GCRPC", "ZipID", "ZipCd", where);
                         
             
           }
            
       }

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2011, 05:13 AM
Hello Jagat,

You can attach SelectedIndexChanged event to the DropDownList and access the selected value as shown below.

aspx:
<EditItemTemplate>
     <asp:DropDownList ID="ddlAccountLookup" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlAccountLookup_SelectedIndexChanged">
     </asp:DropDownList>
</EditItemTemplate>

C#:
protected void ddlAccountLookup_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddl = (DropDownList)sender;
   GridEditableItem item = (GridEditableItem)ddl.NamingContainer;
   string str = ddl.SelectedValue;
}

Thanks,
Shinu.
0
Jagat
Top achievements
Rank 1
answered on 02 Nov 2011, 06:46 PM
Hi Shinu,

This event does not fire at all

Edit: Ignore this post. See my next post.
0
Jagat
Top achievements
Rank 1
answered on 02 Nov 2011, 07:14 PM
Shinu,

If when the grid loads teh city name for the first row is New York.
When i edit this row, the dropdown list should display "New York" in that row. Then the user wil lselect to change to any other value he wants.

Right now, dropdown is populated, but it is not showing the current value when it goes into edit mode.
0
Russ
Top achievements
Rank 1
answered on 02 Nov 2011, 08:11 PM
In vb.net it would be

ddlCity.SelectedValue = directcast(e.item.DataItem, DataRowView).Item("City")

In C#:

ddlCity.SelectedValue = (DataRowView)e.item.DataItem["City"];
0
Jagat
Top achievements
Rank 1
answered on 02 Nov 2011, 08:47 PM
Russ,

Which event should i have it in?
I tried in edit_command, itemdatabound events.


I am getting this error message:

'Telerik.Web.UI.GridItemEventArgs' does not contain a definition for 'item' and no extension method 'item' accepting a
first argument of type 'Telerik.Web.UI.GridItemEventArgs' could be found (are you missing a using directive or an assembly reference?) 

 

0
Shinu
Top achievements
Rank 2
answered on 03 Nov 2011, 10:41 AM
Hello Jagat,

You can take a look into the following documentation.
Operations with MS DropDownList in EditItemTemplate of GridTemplateColumn

Thanks,
Shinu.
Tags
Grid
Asked by
Jagat
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jagat
Top achievements
Rank 1
Russ
Top achievements
Rank 1
Share this question
or