asp.net 自定义日期列telerik网格的筛选选项

ekqde3dh  于 2023-01-14  发布在  .NET
关注(0)|答案(3)|浏览(115)

我正在使用telerik网格。我需要对网格中的所有列应用过滤器。目前我正在使用以下代码自定义过滤器选项。通过使用以下代码,我正在删除所有列的某些项。但是,对于日期列,请任何人告诉我在网格中过滤的可能选项是什么,以及如何自定义这些过滤选项?

代码隐藏

protected void RGVTest_Init(object sender, EventArgs e)
    {

        GridFilterMenu menu = RGVTest.FilterMenu;
        int i = 0;
        while (i < menu.Items.Count)
        {
            if (menu.Items[i].Text == "Between" ||
                menu.Items[i].Text == "NotBetween")
            {
                menu.Items.RemoveAt(i);
            }
            else
            {
                i++;
            }
        }
    }

Aspx:

<telerik:RadGrid ID="RGVTest" runat="server" Skin="Vista" AllowPaging="True"
            AllowFilteringByColumn="true" AllowSorting="true" GridLines="None" OnItemCommand="RGVTest_ItemCommand"
            PageSize="10" OnNeedDataSource="RGVTest_NeedDataSource" OnItemDataBound="RGVTest_ItemDataBound"
            OnInit="RGVTest_Init">
            <GroupingSettings CaseSensitive="false" />
            <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
            <MasterTableView AutoGenerateColumns="False" CellSpacing="-1" >
                <NoRecordsTemplate>
                    <div style="color: red">
                        No Records to display!
                    </div>
                </NoRecordsTemplate>
                <Columns>

                    <telerik:GridTemplateColumn DataField="SSN" ReadOnly="True" HeaderText="SSN" UniqueName="SSN"
                        SortExpression="SSN">
                        <ItemTemplate>
                            <asp:Label ID="LblSSN" runat="server" Text='<%#Eval("SSN") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Left" Width="5%" />
                    </telerik:GridTemplateColumn>

                    <telerik:GridTemplateColumn DataField="Date" HeaderText="Date" UniqueName="Date"
                        SortExpression="Date">
                        <ItemTemplate>
                            <asp:Label ID="LblDate" runat="server" Text='<%#Eval("Date","{0:MM/dd/yyyy}") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Left" Width="4%" />
                    </telerik:GridTemplateColumn>

                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
af7jpaap

af7jpaap1#

你喜欢这样

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
 {  
     if (e.CommandName == RadGrid.FilterCommandName)  
     {  
         Pair filterPair = e.CommandArgument as Pair;  
         string columnName = Convert.ToString(filterPair.Second);  
         if (columnName == "CreationDate")  
         {  
            e.Canceled = true;  
             string date = ((TextBox)((GridFilteringItem)e.Item)[Convert.ToString(filterPair.Second)].Controls          [0]).Text;  
             DateTime startDate = Convert.ToDateTime(date);  
             DateTime endDate = startDate.AddDays(1);  
             string newFilter = "('" + startDate.ToString("MM/dd/yyyy") + "' <= [CreationDate] AND [CreationDate] <= '"         + endDate.ToString("MM/dd/yyyy") + "')";   
             GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);  
             dateColumn.CurrentFilterValue = startDate.ToString("MM/dd/yyyy");  
            RadGrid1.MasterTableView.FilterExpression = newFilter;  
             RadGrid1.Rebind();  
         }  
     }  
 }
igetnqfo

igetnqfo2#

像这样试试

function FilterMenuShowing(sender, eventArgs)   
   {     
       if (eventArgs.get_column().get_uniqueName() == "IsPostable")      
       {              
           var menu = eventArgs.get_menu();   
           var items = menu._itemData;   

           var i = 0;   

           while (i < items.length)     
           {     
               if (items[i].value != "NoFilter" && items[i].value != "EqualTo" && items[i].value != "NotEqualTo")     
               {     
                   var item = menu._findItemByValue(items[i].value);   
                   if (item != null)   
                       item._element.style.display="none";   
               }     
               i++;     
           }
       }
       else
       {
           var menu = eventArgs.get_menu();   
           var items = menu._itemData;   

           var i = 0;
           while (i < items.length)     
           {     
               var item = menu._findItemByValue(items[i].value);   
               if (item != null)   
                   item._element.style.display="";   
               i++;     
           }
       }     

   }

在你的坐标上加上-

<ClientSettings>  
    <ClientEvents OnFilterMenuShowing="filterMenuShowing" />  
</ClientSettings>

参见详细信息here

ylamdve6

ylamdve63#

You can try the way its done below for "order date" column.

protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName &&
    ((Pair)e.CommandArgument).Second.ToString() == "OrderDate"
     &&
    ((Pair)e.CommandArgument).First != "NoFilter")
    {
        e.Canceled = true;
        GridFilteringItem filterItem = (GridFilteringItem)e.Item;
        string currentPattern = (filterItem[((Pair)e.CommandArgument).Second.ToString()].Controls[0] as TextBox).Text;
        string filterPattern = "";
        string filterPatternAssist = "";
        if (currentPattern.IndexOf(" ") != -1)
        {
            currentPattern = currentPattern.Replace(" ", "/");
        }
        string[] vals = currentPattern.Split("/".ToCharArray());
        string filterOption = (e.CommandArgument as Pair).First.ToString();
        if (filterOption != "IsNull" && filterOption != "NotIsNull")
        {
            if (vals.Length > 3)
            {
                filterPatternAssist = vals[4] + "/" + vals[3] + "/" + vals[5];
            }
            filterPattern = vals[1] + "/" + vals[0] + "/" + vals[2];
        }
        GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
        switch (filterOption)
        {
            case "EqualTo":
                filterPattern = "[OrderDate] = '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
                break;
            case "NotEqualTo":
                filterPattern = "Not [OrderDate] = '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;
                break;
            case "GreaterThan":
                filterPattern = "[OrderDate] > '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThan;
                break;
            case "LessThan":
                filterPattern = "[OrderDate] < '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.LessThan;
                break;
            case "GreaterThanOrEqualTo":
                filterPattern = "[OrderDate] >= '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                break;
            case "LessThanOrEqualTo":
                filterPattern = "[OrderDate] <= '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;
                break;
            case "Between":
                filterPattern = "'" + filterPattern + "' <= [OrderDate] AND [OrderDate] <= '" + filterPatternAssist + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.Between;
                break;
            case "NotBetween":
                filterPattern = "[OrderDate] <= '" + filterPattern + "' OR [OrderDate] >= '" + filterPatternAssist + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.NotBetween;
                break;
            case "IsNull":
                break;
            case "NotIsNull":
                break;
        }
        foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
        {
            if (column.UniqueName != "OrderDate")
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
        }
        Session["filterPattern"] = filterPattern;
        dateColumn.CurrentFilterValue = currentPattern;
        filterItem.OwnerTableView.Rebind();
    }
    //Add more conditional checks for commands here if necessary
    else if (e.CommandName != RadGrid.SortCommandName && e.CommandName != RadGrid.PageCommandName)
    {
        Session["filterPattern"] = null;
        GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
        dateColumn.CurrentFilterFunction = GridKnownFunction.NoFilter;
        dateColumn.CurrentFilterValue = string.Empty;
    }
}

相关问题