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

Filter by multiple selections

1 Answer 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Danny
Top achievements
Rank 1
Danny asked on 05 Nov 2012, 03:48 PM
Does RadGrid support filtering by multiple selections such as check boxes (see attached screen shot)? If it does, Can someone provide some sample code for that?

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Nov 2012, 06:25 AM
Hi,

Please take a look in to the following code for filtering RadGrid using Multi-Selection RadComboBox.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnPreRender="RadGrid1_PreRender" AutoGenerateColumns="False"
    OnNeedDataSource="RadGrid1_NeedDataSource" EnableLinqExpressions="false">
    <mastertableview editmode="InPlace" allowfilteringbycolumn="true" commanditemdisplay="Top">
            <Columns>
                <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID"
                    UniqueName="EmployeeID" DataType="System.Int32">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1"
                            DataValueField="EmployeeID" DataTextField="EmployeeID" EmptyMessage="All Types"
                            AllowCustomText="true" Width="240px">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" Checked="true" ID="chk1" onclick="onCheckBoxClick(this)" />
                                <%# Eval("EmployeeID")%>
                            </ItemTemplate>
                            <FooterTemplate>
                                 <asp:Button ID="clrFilters" runat="server" Text="Clear filters" 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("EmployeeID," + text);
 
                                }
                                function removeLastComma(str) {
                                    return str.replace(/,$/, "");
                                }
                                                         
                            </script>
 
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City"
                    UniqueName="City">
                </telerik:GridBoundColumn>
            </Columns>
        </mastertableview>
    <clientsettings>
            <Selecting AllowRowSelect="true" />
        </clientsettings>
</telerik:RadGrid>
<br />
<asp:Button ID="clrFilters" runat="server" Text="Clear filters" OnClick="clrFilters_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT * FROM [Employees]"></asp:SqlDataSource>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1"
    OnAjaxRequest="RadAjaxManager1_AjaxRequest">
    <ajaxsettings>
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="clrFilters">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </ajaxsettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default" />

C#:
protected void Page_Load(object sender, EventArgs e)
{
    ViewState["filterRawString"] = null;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (ViewState["filterRawString"] != null)
    {
        foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
        {
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
            foreach (RadComboBoxItem comboItem in combo.Items)
            {
                if (ViewState["filterRawString"].ToString().Contains(comboItem.Text.ToString()))
                {
                    CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
                    chk.Checked = true;
                }
                else
                {
                    CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
                    chk.Checked = false;
                }
            }
        }
    }
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
    string str = e.Argument.ToString();
    String query = "SELECT EmployeeID, City FROM Employees WHERE EmployeeID = '" + str.Split(',')[1] + "'";
    if (str.Split(',')[0] == "EmployeeID")
    {
        for (int i = 2; i < str.Split(',').Length; i++)
        {
            query = query + " OR EmployeeID='" + str.Split(',')[i] + "'";
 
        }
        ViewState["filterRawString"] = str;
        RadGrid1.DataSource = GetDataTable(query);
        RadGrid1.Rebind();
 
        GridFilteringItem filterItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
        RadComboBox combo = (RadComboBox)filterItem.FindControl("RadComboBox1");
        RadAjaxManager1.ResponseScripts.Add("$find('" + combo.ClientID + "').showDropDown();");
    }
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    if (ViewState["filterRawString"] == null)
        RadGrid1.DataSource = GetDataTable("SELECT EmployeeID,City FROM Employees");
}
protected void clrFilters_Click(object sender, EventArgs e)
{
    foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
    {
        column.CurrentFilterFunction = GridKnownFunction.NoFilter;
        column.CurrentFilterValue = string.Empty;
    }
    RadGrid1.MasterTableView.FilterExpression = string.Empty;
    RadGrid1.MasterTableView.Rebind();
}


Please take a look into this code library.

Thanks,
Shinu.
Tags
Grid
Asked by
Danny
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or