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

RadGrid multi-select not rebinding

3 Answers 78 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zeyad
Top achievements
Rank 1
Zeyad asked on 20 Dec 2013, 12:09 AM
Hello all,

I have a datagrid where i am using multiple filtering and each one contains a dropdown list for multiple selection, the thing is when i change the checkboxes the code is firing, but the datagrid is not showing the changes and not rebinding.

  <telerik:RadGrid ID="GridMedical"  runat="server" ShowStatusBar="True" 
              OnPreRender="RadGrid1_PreRender"
             
              OnNeedDataSource="RadGrid1_NeedDataSource"
        AutoGenerateColumns="False" PageSize="20" AllowSorting="True" AllowAutomaticInserts="False"
        AllowPaging="True"  AllowAutomaticDeletes="False"
            
        AllowAutomaticUpdates="False" Width="835px" FilterType="CheckedList" AllowFilteringByColumn="True"
                    GridLines="None" CellSpacing="0">
                    <ExportSettings>
                        <Pdf>
                            <PageHeader>
                                <LeftCell Text="" />
                                <MiddleCell Text="" />
                                <RightCell Text="" />
                            </PageHeader>
                            <PageFooter>
                                <LeftCell Text="" />
                                <MiddleCell Text="" />
                                <RightCell Text="" />
                            </PageFooter>
                        </Pdf>
                    </ExportSettings>
                     
                    <MasterTableView  InsertItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnFirstPage" DataKeyNames="StudentID" EditMode="EditForms" AutoGenerateColumns="false" AllowMultiColumnSorting="False" Width="100%" CommandItemDisplay="Top" Name="medStudents">
                    <CommandItemSettings  ShowExportToPdfButton="true" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
                            Visible="True">
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
                            Visible="True">
                        </ExpandCollapseColumn>
                    <Columns>
                     
                     
                    <telerik:GridBoundColumn SortExpression="StudentID" ShowFilterIcon="false" AllowFiltering="false" HeaderText="#" HeaderButtonType="TextButton" DataField="StudentID" UniqueName="StudentID" MaxLength="7">
                     
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="StudentName" HeaderText="Name" HeaderButtonType="TextButton" DataField="StudentName" UniqueName="StudentName" MaxLength="40">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="Grade"  HeaderText="Gr" HeaderButtonType="TextButton" DataField="Grade" UniqueName="Grade">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RadComboGrades" runat="server" DataSourceID="SqlDataSourceFilterGrades"
                            DataValueField="Grd" DataTextField="Grd" EmptyMessage="All"
                            AllowCustomText="true" Width="60px">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" Checked="true" ID="grades_chk1" onclick="onCheckBoxClickGrades(this)" />
                                <%# Eval("Grd")%>
                            </ItemTemplate>
                            <FooterTemplate>
                                 <asp:Button ID="clrFiltersGrades" runat="server" Text="Clear" OnClick="clrFiltersGrades_Click" />
                            </FooterTemplate>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlock2" runat="server">
  
                            <script type="text/javascript">
                                function onCheckBoxClickGrades(chk) {
                                    var text = "", values = "";
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                    var combo = $find('<%# ((GridItem)Container).FindControl("RadComboGrades").ClientID %>');
                                    //get the collection of all items
                                    var items = combo.get_items();
                                    //enumerate all items
                                    for (var i = 0; i < items.get_count(); i++) {
                                        var item = items.getItem(i);
                                        //get the checkbox element of the current item
                                        var chk1 = $get(combo.get_id() + "_i" + i + "_grades_chk1");
                                        if (chk1.checked) {
                                            text += item.get_text() + ",";
                                            values += item.get_value() + ",";
                                        }
                                    }
                                    //remove the last comma from the string
                                    text = removeLastComma(text);
                                    values = removeLastComma(values);
 
                                    $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Grades," + values);
 
                                }
                                function removeLastComma(str) {
                                    return str.replace(/,$/, "");
                                }
                                                          
                            </script>
  
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="Class"  HeaderText="Cl" HeaderButtonType="TextButton" DataField="Class" UniqueName="Class">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RadComboClasses" runat="server" DataSourceID="SqlDataSourceFilterGrades"
                            DataValueField="Grd" DataTextField="Grd" EmptyMessage="All"
                            AllowCustomText="true" Width="60px">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" Checked="true" ID="classes_chk1" onclick="onCheckBoxClickClasses(this)" />
                                <%# Eval("Grd")%>
                            </ItemTemplate>
                            <FooterTemplate>
                                 <asp:Button ID="clrFiltersClasses" runat="server" Text="Clear" OnClick="clrFiltersGrades_Click" />
                            </FooterTemplate>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
  
                            <script type="text/javascript">
                                function onCheckBoxClickClasses(chk) {
                                    var text = "", values = "";
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                    var combo = $find('<%# ((GridItem)Container).FindControl("RadComboClasses").ClientID %>');
                                    //get the collection of all items
                                    var items = combo.get_items();
                                    //enumerate all items
                                    for (var i = 0; i < items.get_count(); i++) {
                                        var item = items.getItem(i);
                                        //get the checkbox element of the current item
                                        var chk1 = $get(combo.get_id() + "_i" + i + "_classes_chk1");
                                        if (chk1.checked) {
                                            text += item.get_text() + ",";
                                            values += item.get_value() + ",";
                                        }
                                    }
                                    //remove the last comma from the string
                                    text = removeLastComma(text);
                                    values = removeLastComma(values);
 
                                    $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Classes," + values);
 
                                }
                                function removeLastComma(str) {
                                    return str.replace(/,$/, "");
                                }
                                                          
                            </script>
  
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="MedicalName"  HeaderText="Issue" HeaderButtonType="TextButton" DataField="MedicalName" UniqueName="MedicalName">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSourceFilterMedical"
                            DataValueField="MedID" DataTextField="MedicalName" EmptyMessage="All"
                            AllowCustomText="true" Width="90px">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" Checked="true" ID="chk1" onclick="onCheckBoxClick(this)" />
                                <%# Eval("MedicalName")%>
                            </ItemTemplate>
                            <FooterTemplate>
                                 <asp:Button ID="clrFilters" runat="server" Text="Clear" OnClick="clrFilters_Click" />
                            </FooterTemplate>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
  
                            <script type="text/javascript">
                                function onCheckBoxClick(chk) {
                                    var text = "", values = "";
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                    var combo = $find('<%# ((GridItem)Container).FindControl("RadComboBox1").ClientID %>');
                                    //get the collection of all items
                                    var items = combo.get_items();
                                    //enumerate all items
                                    for (var i = 0; i < items.get_count(); i++) {
                                        var item = items.getItem(i);
                                        //get the checkbox element of the current item
                                        var chk1 = $get(combo.get_id() + "_i" + i + "_chk1");
                                        if (chk1.checked) {
                                            text += item.get_text() + ",";
                                            values += item.get_value() + ",";
                                        }
                                    }
                                    //remove the last comma from the string
                                    text = removeLastComma(text);
                                    values = removeLastComma(values);
 
                                    $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("MedicalName," + values);
 
                                }
                                function removeLastComma(str) {
                                    return str.replace(/,$/, "");
                                }
                                                          
                            </script>
  
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="MedLevel" HeaderText="Level" HeaderButtonType="TextButton" DataField="MedLevel" UniqueName="MedLevel">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="Notes" HeaderText="Notes" HeaderButtonType="TextButton" DataField="Notes" UniqueName="Notes">
                    </telerik:GridBoundColumn>
                     
                     
                     
                    <telerik:GridBoundColumn SortExpression="HospitalName" HeaderText="Hospital" HeaderButtonType="TextButton" AllowFiltering="false" DataField="HospitalName" UniqueName="HospitalName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="FileNumber" HeaderText="File #" HeaderButtonType="TextButton" AllowFiltering="false" DataField="FileNumber" UniqueName="FileNumber">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="PhoneNumber" HeaderText="Phone" HeaderButtonType="TextButton" AllowFiltering="false" DataField="PhoneNumber" UniqueName="PhoneNumber">
                    </telerik:GridBoundColumn>
                     
                    </Columns>
                     
                      
                        <BatchEditingSettings EditType="Cell" />
                        <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" />
                    </MasterTableView>
                    <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" />
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                    </telerik:RadGrid>
                     
                    <asp:SqlDataSource ID="SqlDataSourceFilterGrades" runat="server"
          ConnectionString="<%$ ConnectionStrings:EduZeeRep.Properties.Settings.EschoolTestV3Reporting %>"
          SelectCommand="SELECT DISTINCT CAST(Grade as int) AS Grd FROM AttStudents ORDER BY CAST(Grade as int) ASC">
        
    </asp:SqlDataSource>
                    <asp:SqlDataSource ID="SqlDataSourceFilterMedical" runat="server"
          ConnectionString="<%$ ConnectionStrings:EduZeeRep.Properties.Settings.EschoolTestV3Reporting %>"
          SelectCommand="SELECT DISTINCT MedID,MedicalName FROM EscMedicalIssues">
        
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceMedicalIssues" runat="server"
          ConnectionString="<%$ ConnectionStrings:EduZeeRep.Properties.Settings.EschoolTestV3Reporting %>"
          SelectCommand="SELECT     AttStudents.EnName + ' ' + AttStudents.FamilyEnName AS StudentName, AttStudents.StudentID, EscMedicalIssues.MedicalName, AttStudents.Class,
                      AttStudents.Grade, EscMedicalLinks.MedLevel, EscMedicalLinks.HospitalName, EscMedicalLinks.FileNumber, EscMedicalLinks.PhoneNumber,
                      EscMedicalLinks.Notes
FROM         AttStudents INNER JOIN
                      EscMedicalLinks ON AttStudents.StudentID = EscMedicalLinks.StudentID INNER JOIN
                      EscMedicalIssues ON EscMedicalLinks.MedID = EscMedicalIssues.MedID">
        
    </asp:SqlDataSource>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1"
    OnAjaxRequest="RadAjaxManager1_AjaxRequest">
    <ajaxsettings>
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="GridMedical" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="clrFilters">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="GridMedical" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </ajaxsettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default" />

The code

protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (ViewState["filterRawString"] != null)
            {
                foreach (GridFilteringItem item in GridMedical.MasterTableView.GetItems(GridItemType.FilteringItem))
                {
                    RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
                    foreach (RadComboBoxItem comboItem in combo.Items)
                    {
                        if (ViewState["filterRawString"].ToString().Contains(comboItem.Value.ToString()))
                        {
                            CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
                            chk.Checked = true;
                        }
                        else
                        {
                            CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
                            chk.Checked = false;
                        }
                    }
                    RadComboBox comboGrades = (RadComboBox)item.FindControl("RadComboBoxGrades");
                    foreach (RadComboBoxItem comboItemGrade in comboGrades.Items)
                    {
                        if (ViewState["filterRawString"].ToString().Contains(comboItemGrade.Value.ToString()))
                        {
                            CheckBox chk = (CheckBox)comboItemGrade.FindControl("grades_chk1");
                            chk.Checked = true;
                        }
                        else
                        {
                            CheckBox chk = (CheckBox)comboItemGrade.FindControl("grades_chk1");
                            chk.Checked = false;
                        }
                    }
                }
            }
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string str = e.Argument.ToString();
            DataSet _dsUs = new DataSet();
 
            //String query = "SELECT usr.*, usr.FirstNameEn+' '+usr.FamilyNameEn AS EnglishName, dept.DepEn AS DepartID,dept.DepID AS DeparID,sub.SubEn,sub.SubID,ad.ADID, ad.ADEn,st.StageID,st.StageEn FROM AttUsers usr,AttStages st, AttAcademicalDegrees ad, AttDepartments dept, AttSubjects sub WHERE st.StageID=usr.Stage AND ad.ADID=usr.AcademicalDegree AND sub.SubID=usr.Subject AND usr.Department=dept.DepID AND sub.SubEn='" + str.Split(',')[1] +"'";
             
 
            if (str.Split(',')[0] == "MedicalName")
            {
                String query = "SELECT     AttStudents.EnName + ' ' + AttStudents.FamilyEnName AS StudentName, AttStudents.StudentID, EscMedicalIssues.MedicalName, AttStudents.Class, AttStudents.Grade, EscMedicalLinks.MedLevel, EscMedicalLinks.HospitalName, EscMedicalLinks.FileNumber, EscMedicalLinks.PhoneNumber, EscMedicalLinks.Notes FROM         AttStudents INNER JOIN EscMedicalLinks ON AttStudents.StudentID = EscMedicalLinks.StudentID INNER JOIN EscMedicalIssues ON EscMedicalLinks.MedID = EscMedicalIssues.MedID WHERE EscMedicalIssues.MedID='" + str.Split(',')[1] + "'";
                for (int i = 2; i < str.Split(',').Length; i++)
                {
                    query = query + " OR EscMedicalIssues.MedID='" + str.Split(',')[i] + "'";
 
                }
                ViewState["filterRawString"] = str;
                _dsUs = GetSQLDataSet(query);
                GridMedical.DataSource = _dsUs.Tables[0];
                GridMedical.Rebind();
                 
                GridFilteringItem filterItem = GridMedical.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
                RadComboBox combo = (RadComboBox)filterItem.FindControl("RadComboBox1");
                RadAjaxManager1.ResponseScripts.Add("$find('" + combo.ClientID + "').showDropDown();");
            }
            if (str.Split(',')[0] == "Grades")
            {
                String query = "SELECT     AttStudents.EnName + ' ' + AttStudents.FamilyEnName AS StudentName, AttStudents.StudentID, EscMedicalIssues.MedicalName, AttStudents.Class, AttStudents.Grade, EscMedicalLinks.MedLevel, EscMedicalLinks.HospitalName, EscMedicalLinks.FileNumber, EscMedicalLinks.PhoneNumber, EscMedicalLinks.Notes FROM         AttStudents INNER JOIN EscMedicalLinks ON AttStudents.StudentID = EscMedicalLinks.StudentID INNER JOIN EscMedicalIssues ON EscMedicalLinks.MedID = EscMedicalIssues.MedID WHERE AttStudents.Grade='" + str.Split(',')[1] + "'";
                for (int i = 2; i < str.Split(',').Length; i++)
                {
                    query = query + " OR AttStudents.Grade='" + str.Split(',')[i] + "'";
 
                }
                ViewState["filterRawString"] = str;
                _dsUs = GetSQLDataSet(query);
                GridMedical.DataSource = _dsUs.Tables[0];
                GridMedical.Rebind();
 
                GridFilteringItem filterItem = GridMedical.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
                RadComboBox combo = (RadComboBox)filterItem.FindControl("RadComboBoxGrades");
                RadAjaxManager1.ResponseScripts.Add("$find('" + combo.ClientID + "').showDropDown();");
            }
        }
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            DataSet _dsUsrs = new DataSet();
            _dsUsrs = GetSQLDataSet("SELECT     AttStudents.EnName + ' ' + AttStudents.FamilyEnName AS StudentName, AttStudents.StudentID, EscMedicalIssues.MedicalName, AttStudents.Class, AttStudents.Grade, EscMedicalLinks.MedLevel, EscMedicalLinks.HospitalName, EscMedicalLinks.FileNumber, EscMedicalLinks.PhoneNumber, EscMedicalLinks.Notes FROM         AttStudents INNER JOIN EscMedicalLinks ON AttStudents.StudentID = EscMedicalLinks.StudentID INNER JOIN EscMedicalIssues ON EscMedicalLinks.MedID = EscMedicalIssues.MedID");
            if (ViewState["filterRawString"] == null)
            {
                GridMedical.DataSource = _dsUsrs.Tables[0];
            }
        }
        protected void clrFilters_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in GridMedical.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            GridMedical.MasterTableView.FilterExpression = string.Empty;
            GridMedical.MasterTableView.Rebind();
        }
        protected void clrFiltersGrades_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in GridMedical.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            GridMedical.MasterTableView.FilterExpression = string.Empty;
            GridMedical.MasterTableView.Rebind();
        }



Many thanks

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Dec 2013, 01:56 PM
Hi Zeyad,

I tried your code snippet and i noticed that the RadComboBox ID at code behind is mistyped. Please correct those and the code works fine. For example in aspx its ID="RadComboGrades" and in the code behind you have used RadComboBoxGrades. Please correct all these ID and let me know if any concerns.

Thanks,
Princy



0
Zeyad
Top achievements
Rank 1
answered on 20 Dec 2013, 04:17 PM
Thanks Princy,

It worked, but now the problem is once i select a filter for exmaple grades, i deselct grade 4,5 , and then i try to apply another filter, the previous filter is no longer applied, is there any method i can go through to save the previous filter?

I tried to save in a session and then re-extract it, but it didn't work as well.


Please advise.


Many thanks
0
Eyup
Telerik team
answered on 24 Dec 2013, 02:59 PM
Hi Zeyad,

I highly suggest you to check the options provided in the following post and choose the one most convenient to your specific requirements:
http://www.telerik.com/community/forums/aspnet-ajax/grid/handle-multi-selection-filtering-in-a-custom-column-class.aspx#2810527

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Zeyad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Zeyad
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or