Search code examples
c#asp.netwebformstelerikradgrid

RadGrid in edit mode with RadComboBox with load on demand


I have a Telerik RadGrid where I've put in the EditItemTemplate a RadComboBox (ID:DdlProducts) to load (on demand with filtering) a set of options.

Everything works fine except when I try to edit a row:

I'm not able to preset the selected value of the RadComboBox, because of the LoadOnDemand enabled.

I've followed several suggestions and example on the Telerik website, but obviously none of them works as expected.

Here is my code.

<telerik:RadGrid ID="GrdTopTen" runat="server" OnNeedDataSource="GrdTopTen_NeedDataSource" AutoGenerateColumns="false"
    ShowStatusBar="true" OnUpdateCommand="GrdTopTen_UpdateCommand" OnInsertCommand="GrdTopTen_InsertCommand" OnDeleteCommand="GrdTopTen_DeleteCommand" OnItemDataBound="GrdTopTen_ItemDataBound"
    AllowAutomaticUpdates="false" AllowAutomaticInserts="false" AllowAutomaticDeletes="false">
    <MasterTableView AllowFilteringByColumn="false" EditMode="InPlace" DataKeyNames="TopTenID" CommandItemDisplay="TopAndBottom">
        <Columns>
            <telerik:GridBoundColumn DataField="TopTenID" DataType="System.Int32" HeaderText="ID" ReadOnly="True" UniqueName="TopTenID" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ProdID" DataType="System.Int32" UniqueName="ProdID" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridNumericColumn AllowOutOfRangeAutoCorrect="true" DataField="TopTenPosition" DataType="System.Int32" MaxValue="25" MinValue="1" DecimalDigits="0" ShowSpinButtons="true"></telerik:GridNumericColumn>
            <telerik:GridTemplateColumn DataField="Title" HeaderText="Titolo" UniqueName="Title">
                <ItemTemplate>
                    <%# Eval("Title") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="DdlProducts" DataTextField="Title" DataValueField="ProdID" AutoPostBack="true" Width="350px"
                        EmptyMessage="Selezionare un titolo..." ItemsPerRequest="20" MinFilterLength="3" Filter="Contains"
                        EnableLoadOnDemand="True" HighlightTemplatedItems="true"
                        OnSelectedIndexChanged="DdlProducts_SelectedIndexChanged" OnItemsRequested="DdlProducts_ItemsRequested">
                    </telerik:RadComboBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn CommandName="Delete" Text="Elimina" UniqueName="DeleteColumn" ButtonType="FontIconButton"
                ConfirmText="Sei sicuro di voler cancellare questa voce?" ConfirmDialogType="RadWindow" />
            <telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                HeaderText="Modifica" HeaderStyle-Width="100px" ButtonType="FontIconButton">
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

code behind:

protected void GrdTopTen_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        if (!(e.Item is IGridInsertItem))
        {
            RadComboBox combo = RadComboBox)item.FindControl("DdlProducts");

            RadComboBoxItem preselectedItem = new RadComboBoxItem();

            preselectedItem.Text = item["Title"].Text;
            preselectedItem.Value = item["ProdID"].Text;
            //the above lines doesn't contains any value, are empty.

            combo.Items.Insert(0, preselectedItem);
            combo.SelectedIndex = 0;
        }
    }
}

protected void GrdTopTen_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    using (EcommerceEntities db = new EcommerceEntities())
    {
        var prods = from ttp in db.TopTenProducts
                    join p in db.Products on ttp.ProdID equals p.prodID
                    join pd in db.ProductDescriptions on p.prodID equals pd.prodID
                    where p.prodParent == null && pd.langID==1
                    orderby ttp.TopTenPosition
                    select new
                    {
                        ProdID = p.prodID,
                        Title = pd.prodName,
                        ISBN = p.prodISBN,
                        ttp.TopTenPosition,
                        ttp.TopTenID
                    };
        GrdTopTen.DataSource = prods.ToList();
    }
}

protected void DdlProducts_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    using (EcommerceEntities db = new EcommerceEntities())
    {
        string filter = e.Text;
        var prods = from p in db.Products
                    join pd in db.ProductDescriptions on p.prodID equals pd.prodID
                    where p.prodParent == null && pd.langID == 1 && pd.prodName.Contains(e.Text)
                    select new
                    {
                        ProdID = p.prodID,
                        Title = pd.prodName,
                        ISBN = p.prodISBN
                    };

        RadComboBox comboBox = (RadComboBox)sender;
        // Clear the default Item that has been re-created from ViewState at this point.
        comboBox.Items.Clear();

        foreach (var p in prods)
        {
            RadComboBoxItem item = new RadComboBoxItem();
            item.Text = p.Title;
            item.Value = p.ProdID.ToString();
            item.Attributes.Add("ISBN", p.ISBN);
            comboBox.Items.Add(item);
            item.DataBind();
        }

    }
}

I'm using the Q1 2017 version of Telerik UI ASP.NET


Solution

  • Finally, I've solved by retrieving the row values using the DataBinder.Eval method:

    preselectedItem.Text = DataBinder.Eval(item.DataItem, "Title").ToString();                
    preselectedItem.Value = DataBinder.Eval(item.DataItem, "ProdID").ToString();
    

    It's incredible how buggy is the online documentation of Telerik!