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

3 posts, 1 answers
  1. Steve
    Steve avatar
    84 posts
    Member since:
    Aug 2013

    Posted 25 Aug 2014 Link to this post

    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. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Aug 2014 in reply to Steve Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Steve
    Steve avatar
    84 posts
    Member since:
    Aug 2013

    Posted 26 Aug 2014 Link to this post

    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();
Back to Top