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

radcombobox in edit mode from code behind

3 Answers 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dawson
Top achievements
Rank 1
Dawson asked on 09 Oct 2013, 01:10 PM

Hi,

How to populate a radcombobox in edit mode from code behind. I tried hard its not populating.

Thanks,
Dawson.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Oct 2013, 01:15 PM
Hi Dawson,

Please try the following code snippet to bind the RadComboBox from code behind in editmode.

ASPX:
<telerik:GridTemplateColumn HeaderText="ShipCountry" DataField="ShipCountry">
    <ItemTemplate>
        <%# Eval("ShipCountry")%>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="RadCombobox1" runat="server">
        </telerik:RadComboBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>
 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>" SelectCommand="SELECT distinct [ShipCountry] FROM [Orders]"></asp:SqlDataSource>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            RadComboBox combo = (RadComboBox)edit.FindControl("RadCombobox1");
            combo.DataSourceID = "SqlDataSource2";
            combo.DataTextField = "ShipCountry";
            combo.DataValueField = "ShipCountry";         
        }
    }

Thanks,
Princy
0
Dawson
Top achievements
Rank 1
answered on 11 Oct 2013, 07:38 AM

Hi Princy,
Thanks for your code.. It works fine. . 
But I want it to set to the default value it holds.right now its showing the values from start.how to make it select the value it holds for that row.

Thanks again,
Dawson.
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Oct 2013, 07:46 AM
Hi Dawson,

Please add the following code to the ItemDataBound event to get the selected value to be the current value.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
           //Same code as above
            combo.SelectedValue = DataBinder.Eval(edit.DataItem, "ShipCountry").ToString();   
        }
    }

Thanks,
Princy
Tags
Grid
Asked by
Dawson
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dawson
Top achievements
Rank 1
Share this question
or