Grid Filter by combo box crash when filtered item not in the list

1 Answer 81 Views
Grid
Lev
Top achievements
Rank 1
Iron
Lev asked on 23 Mar 2023, 01:55 AM

Hi. I have RadGrid with filter implemented as combo box. On Item command of GridColumnButton I update the record that changes value of the field being filtered by and rebind the grid. If before grid was filtered by the column that is updated and result contains only one record, rebind crashes the application. After record is updated, the only valid value for column "Sections" is Any Sections Not Complete.


                    <telerik:GridButtonColumn UniqueName="ImageButton" CommandName="MarkRecord" ButtonType="ImageButton"
                        ImageUrl="Content/Images/Reverse.jpg" HeaderStyle-Width="60px" Exportable="false">
                    </telerik:GridButtonColumn>
                    <telerik:GridBoundColumn UniqueName="Sections" DataField="Sections" HeaderText="Sections" HeaderStyle-Width="180px" Exportable="false">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="ddlSection" runat="server" DataSource="<%#Sections %>"
                                DataTextField="Name" DataValueField="Name" Width="130px" AppendDataBoundItems="true"
                                SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Sections").CurrentFilterValue %>'
                                OnClientSelectedIndexChanged="SectionsIndexChanged">
                                <Items>
                                    <telerik:RadComboBoxItem Text="All" />
                                </Items>
                            </telerik:RadComboBox>
                            <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
                                <script type="text/javascript">
                                    function SectionsIndexChanged(sender, args) {
                                        var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        alert(args.get_item().get_value());
                                        tableView.filter("Sections", args.get_item().get_value(), "EqualTo");
                                    }
                                </script>
                            </telerik:RadScriptBlock>
                        </FilterTemplate>
                    </telerik:GridBoundColumn>


        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string strNewStatus;
            string btnAction;
            if(e.CommandName == "Complete" || e.CommandName == "Reverse")
            {
                if (e.CommandName == "Complete")
                {
                    strNewStatus = "COMPLETE";
                    btnAction = "Mark Complete";
                }
                else
                {
                    strNewStatus = "PENDING";
                    btnAction = "Reverse Complete";
                }
                GridDataItem dataItem = e.Item as GridDataItem;
                var id = (int)dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["EncounterID"];
                Encounter enc = Encounters.Find(i => i.EncounterID == id);
                string userName = HttpContext.Current.User.Identity.Name.Replace("ROTHMAN\\", String.Empty);
                string retValue;
                retValue = DataAccess.UpdateVisitInfo(Convert.ToInt32(enc.tid), enc.EncounterID, enc.ECWAcct, userName, strNewStatus, enc.notes, ddlSource.SelectedValue);
                if (retValue != "")
                {
                    //handle error
                    e.Canceled = true;
                    radWindowManager.RadAlert(retValue, 280, 100, "Error", null);
                    return;
                }
                retValue = DataAccess.UpdateTracker(userName, enc.EncounterID, Convert.ToInt32(enc.ECWAcct), btnAction, strNewStatus, "", ddlSource.SelectedValue);
                if (retValue != "")
                {
                    e.Canceled = true;
                    radWindowManager.RadAlert(retValue, 280, 100, "Error", null);
                    return;
                }
                // refresh grid
                Session["Encounters"] = null;
                RadGrid1.Rebind();
            }
        }

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 27 Mar 2023, 10:49 AM

Hi Lev,

If you're using an inline expression to bind the Grid's Filter value to the Combo's SelectedValues, you must make sure any value coming from the Grid also exists in the ComboBox, otherwise, the app may throw the "Index out of range" exception.

If the data source of Combo is changing dynamically, or the Grid may have values that are not present in Combo's data source, you should handle the selected value manually without using bind expressions.

Remove the SelectedValue property from the Markup, and pre-select the item in the ItemDataBound event.

For example:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFilteringItem)
    {
        GridFilteringItem filterItem = (GridFilteringItem)e.Item;

        RadComboBox filterCombo = filterItem["Sections"].FindControl("ddlSection") as RadComboBox;

        GridTemplateColumn sectionsColumn = RadGrid1.MasterTableView.GetColumn("Sections") as GridTemplateColumn;

        string currentSectionFilterValue = sectionsColumn.CurrentFilterValue;

        RadComboBoxItem selectedItem = filterCombo.FindItemByValue(currentSectionFilterValue);

        if (selectedItem != null) {
            selectedItem.Selected = true;
        }
    }
}

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Lev
Top achievements
Rank 1
Iron
commented on 28 Mar 2023, 09:19 PM

Thanks for reply. It is almost working. When selecting any value from drop down, grid will filter by that value. But the problem is, that even though it filtering, the selected value of the drop down still "All". And that not allowing to select back all values. No filtering event is firing.


            if (e.Item is GridFilteringItem)
            {
                GridFilteringItem filterItem = (GridFilteringItem)e.Item;
                RadComboBox filterCombo = filterItem["Sections"].FindControl("ddlSection") as RadComboBox;
                GridTemplateColumn sectionsColumn = RadGrid1.MasterTableView.GetColumn("Sections") as GridTemplateColumn;
                if (sectionsColumn != null)
                {
                    string currentSectionFilterValue = sectionsColumn.CurrentFilterValue;
                    RadComboBoxItem selectedItem = filterCombo.FindItemByValue(currentSectionFilterValue);
                    if (selectedItem != null)
                    {
                        selectedItem.Selected = true;
                    }
                }
            }

Attila Antal
Telerik team
commented on 29 Mar 2023, 01:02 PM

You can review the values coming from the Grid, and compare them to the values in the Combo and you will have a good idea of why some values cannot be selected. This is mainly related to the values, and there is no Telerik-specific solution to fix that.

Tags
Grid
Asked by
Lev
Top achievements
Rank 1
Iron
Answers by
Attila Antal
Telerik team
Share this question
or