在RadGrid FilterTemplate中过滤加载数据

时间:2015-10-08 12:43:09

标签: radgrid

我有一个RadGrid,它有一个使用RadComboBox的FilterTemplate。当我在RadGrid中加载数据时,我想显示用户过滤的数据。这是我的专栏:

<telerik:GridTemplateColumn FilterControlAltText="Filter tclmArrangement column" 
 HeaderText="Arrangement" UniqueName="tclmArrangement" DefaultInsertValue="-" 
 DataField="IsDemoAssigned">
    <HeaderStyle Width="2%" />
    <ItemTemplate>
        <asp:Repeater ID="rptchkarrangement" runat="server" 
         DataSource='<%# IIf(DataBinder.Eval(Container, "DataItem.IsDEmoAssigned") = 0, 
         DataBinder.Eval(Container, "DataItem.Employees"), Nothing)%> '>
            <ItemTemplate>
                <table>
                    <tr class="clsParent">
                         <input type="checkbox" class="clsEmployee" id="cbSelect" 
                          checked='<%# DataBinder.Eval(Container, "DataItem.IsAssigned")%>'
                          runat="server" />
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <FilterTemplate>
        <telerik:RadComboBox ID="RadComboBoxAssignmentStatus" Height="80px" Width="80px" 
         AppendDataBoundItems="true" runat="server" 
         OnClientSelectedIndexChanged="AssignmentStatusIndexChanged">
        </telerik:RadComboBox>
        <telerik:RadScriptBlock ID="rsbAssignmentStatus" runat="server">
            <script type="text/javascript">
                function AssignmentStatusIndexChanged(sender, args) {

                    var tableView = $find("<%# TryCast(Container,GridItem)
                                    .OwnerTableView.ClientID %>");
                    var selectedValue = sender.get_value();

                    if (selectedValue) {
                        if (parseInt(selectedValue) < 0) {
                          tableView.filter("tclmArrangement", selectedValue, "NoFilter");
                        }
                        else {
                          tableView.filter("tclmArrangement", selectedValue, "EqualTo");
                        }
                    }
                }

            </script>
        </telerik:RadScriptBlock>
    </FilterTemplate>
</telerik:GridTemplateColumn>

使用“全部”选项最初显示完整数据时,过滤器可正常工作。过滤器具有值(全部,已分配和未分配)。如何在RadGrid中显示属于UnAssigned类别的数据?

2 个答案:

答案 0 :(得分:0)

不确定这是你想要的,但我过滤了GridTemplateColumn DataFields。我已经删除了中继器。

我希望这可以帮到你

的.aspx

<telerik:RadGrid ID="rg" runat="server" AutoGenerateColumns="false" 
 OnNeedDataSource="rg_NeedDataSource" AllowFilteringByColumn="true">
<MasterTableView>
    <Columns>
        <telerik:GridTemplateColumn DataField="IsAssigned" DefaultInsertValue="-" 
         UniqueName="tclmArrangement">
            <HeaderStyle Width="2%" />
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" 
                 Checked='<%# Eval("IsAssigned") %>' />
                <asp:Label ID="lbl" runat="server" Text='<%# Eval("EmployeeName") %>'>
                </asp:Label>
            </ItemTemplate>
            <FilterTemplate>
                <telerik:RadComboBox ID="rcbAssignmentStatus" Height="80px" Width="80px" 
                    runat="server" 
                    OnClientSelectedIndexChanged="AssignmentStatusIndexChanged"
                    SelectedValue='<%# ((GridItem)Container)
                                   .OwnerTableView
                                   .GetColumn("tclmArrangement").CurrentFilterValue %>'>
                    <Items>
                        <telerik:RadComboBoxItem Text="All" Value=""  />
                        <telerik:RadComboBoxItem Text="All Assigned" Value="true" />
                        <telerik:RadComboBoxItem Text="Unassigned" Value="false" />
                    </Items>
                </telerik:RadComboBox>
                <telerik:RadCodeBlock ID="rcb" runat="server">
                    <script type="text/javascript">
                        function AssignmentStatusIndexChanged(sender, args) {
                            var tableView = $find('<%# ((GridItem)Container)
                                            .OwnerTableView.ClientID %>');
                            var value = sender.get_selectedItem().get_value();

                            if (value != "") 
                                tableView.filter('tclmArrangement', args.get_item()
                                .get_value(), 'EqualTo');
                            else 
                                tableView.filter('tclmArrangement', args.get_item()
                                .get_value(), 'NoFilter')
                        }
                    </script>
                </telerik:RadCodeBlock>
            </FilterTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
</MasterTableView>
</telerik:RadGrid>

的.cs

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Variable
        DataTable dt = new DataTable();
        dt.Columns.Add("IsAssigned", typeof(bool));
        dt.Columns.Add("EmployeeName", typeof(string));

        string[] employeeName = { "Patrick", "Bucky", "Henry", "Jesus", "Linda" };
        int[] isAssigned = { 1, 1, 0, 0, 0 };

        // Loop
        for (int i = 0; i < employeeName.Length; i++)
            dt.Rows.Add(isAssigned[i], employeeName[i]);

        ViewState["Data"] = dt;

        rg.DataSource = dt;
        rg.DataBind();

        dt.Dispose();

    }
}

protected void rg_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    rg.DataSource = ViewState["Data"] as DataTable;
}

答案 1 :(得分:0)

这个链接帮助了我。

http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/filtering/how-to/apply-default-filter-on-initial-load

链接中未指定的一件事是设置“EnableLinqExpression = false”

我选择了“在代码隐藏中设置初始过滤器”方法。过滤器表达式可以形成如下。

http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/filtering/how-to/operate-with-the-filterexpression-manually