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

How do I Load Combo with Dictionary, select from grid value

2 Answers 467 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 25 Aug 2014, 05:52 PM
I have a grid that is populated by a SQL Datasource control. When the user edits a row, an edit form template is displayed. Inside this template is a combobox that I populate server-side with values in a dictionary. How do I set the selected value when the edit form template is displayed? Textboxes in the edit form template use the 'Bind' value for their text values, and other comboboxes in the template have a DataSourceID set, so when SelectedValue is set, it's not a problem.

So, I guess my question is multipart.

1. In what event should I be setting the combobox load?
2. For the combobox, how do I set the selected value to be that of the item in the grid?

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Aug 2014, 07:24 AM
Hi Steve,

As for your first question the best approach to load a RadComboBox in RadGrid is try to bind it in the OnItemDataBound event of RadGrid as follows. Please have a look into the sample code snippet to achieve your scenario.

ASPX:
<telerik:RadGrid ID="rgrdOrders" runat="server" AutoGenerateColumns="false" DataSourceID="sqldsOrders" AutoGenerateEditColumn="True" CellSpacing="-1" GridLines="Both" ResolvedRenderMode="Classic" OnItemDataBound="rgrdOrders_ItemDataBound">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="CustomerID" UniqueName="CustomerID" HeaderText="CustomerID">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn>
                <EditItemTemplate>
                    <telerik:RadComboBox ID="rcboOrders" runat="server">
                    </telerik:RadComboBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void rgrdOrders_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        //bind teh radcombobox here
        GridEditableItem item = e.Item as GridEditableItem;
        RadComboBox combo = item.FindControl("rcboOrders") as RadComboBox;
        combo.DataSourceID = "sqldsOrders";
        combo.DataTextField = "OrderID";
        combo.DataValueField = "OrderID";
        //select the combobox item
        TextBox selectedText = (TextBox)item["OrderID"].Controls[0];
        combo.SelectedValue = selectedText.Text;
    }
}

Thanks,
Princy.
0
Steve
Top achievements
Rank 1
answered on 26 Aug 2014, 11:14 AM
Thanks, Princy! That worked perfectly. 

I added the actual value to the key values of the grid, so I was able to add the following code to select from the combo.

combo.SelectedValue = item.GetDataKeyValue("LINE_OF_BUSINESS").ToString();
Tags
ComboBox
Asked by
Steve
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Steve
Top achievements
Rank 1
Share this question
or