You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RadGridView按退格键时筛选不生效问题(C#)

RadGridView Filtering Not Working on Backspace in C#

Hey there, let's figure out why your backspace key isn't triggering the filter update properly. The good news is you don't need a separate function to handle backspace—the issue lies in the logic of your existing TextChanged event handler, which doesn't account for clearing filters when the search text is emptied (like when you backspace to nothing).

What's Wrong with the Current Code?

Looking at your code:

  • You only add a filter when txt_search.Text != "", but you never remove existing filters when the text becomes empty.
  • Every time the text changes, you're adding a new CompositeFilterDescriptor without clearing old ones—this causes filters to stack up, leading to unexpected behavior when you backspace.
  • The manual row visibility logic might conflict with RadGridView's built-in filtering, especially for child templates.

Fixed Code with Explanations

Here's the revised version of your event handler, with key fixes:

private void txt_search_TextChanged_1(object sender, EventArgs e)
{
    try
    {
        var childTemplate = this.radGridView1.MasterTemplate.Templates[0];
        
        // 1. Clear existing filters first to avoid stacking
        childTemplate.FilterDescriptors.Clear();
        this.radGridView1.MasterTemplate.FilterDescriptors.Clear();

        // 2. Configure filtering settings (move this to Form_Load to avoid redundant calls)
        this.radGridView1.EnableFiltering = true;
        childTemplate.EnableFiltering = true;
        childTemplate.ShowFilteringRow = false;
        this.radGridView1.MasterTemplate.ShowFilteringRow = false;
        childTemplate.ShowTotals = true;
        childTemplate.ShowFilterCellOperatorText = false;

        string searchText = txt_search.Text.Trim();
        if (!string.IsNullOrEmpty(searchText))
        {
            // Add the filter only when there's valid search text
            var searchFilter = new CompositeFilterDescriptor();
            searchFilter.FilterDescriptors.Add(new FilterDescriptor("product", FilterOperator.Contains, searchText));
            childTemplate.FilterDescriptors.Add(searchFilter);
        }

        // 3. Optimized row visibility logic (matches filter behavior)
        foreach (GridViewRowInfo row in radGridView1.MasterTemplate.Rows)
        {
            row.IsVisible = row.ChildRows.Count > 0 && 
                           (string.IsNullOrEmpty(searchText) || 
                            row.ChildRows.Any(child => child.Cells["product"].Value?.ToString()?.Contains(searchText) == true));
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Something went wrong searching the grid. Please try again and contact I.T. if this problem persists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Key Fixes Breakdown:

  • Clear Filters First: We clear existing filters on both the master and child templates before adding new ones—this ensures old filters don't linger when you backspace to empty text.
  • Handle Empty Text Properly: When the search box is empty, we don't add any filters, which resets the grid to show all data.
  • Optimize Configuration: The filtering settings (like EnableFiltering, ShowFilteringRow) only need to be set once (e.g., in Form_Load) instead of every time the text changes—this boosts performance.
  • Consistent Row Visibility: The updated loop checks both child row count and search text matches, ensuring parent row visibility aligns with the applied filter.

Why the VB Version Worked

It's likely the VB.NET version either cleared filters before applying new ones or handled empty text explicitly, which your converted C# code missed. The backspace key does trigger TextChanged—the problem was just that your code wasn't responding correctly when the text became empty.

内容的提问来源于stack exchange,提问作者benjiiiii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:21:50