DataGridView列表筛选

以下是实现 DataGridView 列表筛选的解决方案:

解决思路

  1. 创建一个 TextBox 控件,用户可以在其中输入筛选条件。
  2. TextBoxTextChanged 事件添加处理程序,当用户输入文本时,根据输入的文本对 DataGridView 中的数据进行筛选。
  3. 使用 DataView 作为 BindingSource 的数据源,通过设置 DataViewRowFilter 属性实现筛选功能。

实现代码

using System;
using System.Windows.Forms;

public partial class Form1 : Form
{
    private BindingSource bindingSource;
    private DataView dataView;

    public Form1()
    {
        InitializeComponent();
        // 假设你已经有一个 DataGridView 控件名为 dataGridView1
        // 并且已经将其绑定到数据源,例如通过之前提到的 FillDataGridView 方法
        bindingSource = new BindingSource();
        dataView = new DataView();
        bindingSource.DataSource = dataView;
        dataGridView1.DataSource = bindingSource;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // 假设你已经将数据源添加到 dataView 中
        // 这里可以调用之前的方法填充数据,例如 FillDataGridView 方法
        // FillDataGridView();
    }

    private void textBoxFilter_TextChanged(object sender, EventArgs e)
    {
        string filterText = textBoxFilter.Text;
        if (string.IsNullOrEmpty(filterText))
        {
            dataView.RowFilter = "";
        }
        else
        {
            // 假设你要根据某一列进行筛选,例如 sampleName 列
            dataView.RowFilter = $"sampleName LIKE '%{filterText}%'";
        }
    }
}

代码解释

  1. 初始化部分
    • Form1 的构造函数中,创建 BindingSource 实例 bindingSourceDataView 实例 dataView
    • dataView 作为 bindingSource 的数据源,将 bindingSource 作为 dataGridView1 的数据源。
    • Form1_Load 事件中,你可以调用之前的方法(如 FillDataGridView)将数据添加到 dataView 中,这里代码中暂时省略。
  2. 筛选部分
    • 创建一个 TextBox 控件 textBoxFilter,并为其 TextChanged 事件添加 textBoxFilter_TextChanged 处理程序。
    • textBoxFilter_TextChanged 方法中:
      • 获取用户输入的文本 filterText
      • 如果 filterText 为空,则清除 DataViewRowFilter,显示所有数据。
      • 否则,使用 LIKE 操作符对 sampleName 列进行筛选,筛选出包含用户输入文本的行。

使用说明

  1. 在你的 Windows 窗体应用程序中,将上述代码添加到相应的 Form 类中。
  2. 在窗体设计器中添加一个 TextBox 控件,并将其 Name 属性设置为 textBoxFilter,将其 TextChanged 事件关联到 textBoxFilter_TextChanged 方法。
  3. 确保你已经使用 FillDataGridView 或其他方法将数据添加到 dataView 中。
  4. 当用户在 textBoxFilter 中输入文本时,DataGridView 将根据用户输入的文本对 sampleName 列进行筛选,只显示符合条件的行。

优化建议

  1. 可以让用户选择要筛选的列,而不是固定使用 sampleName 列,例如使用一个 ComboBox 控件,让用户选择筛选列,然后根据用户的选择构建 RowFilter
  2. 对于复杂的筛选条件,可以使用多个 TextBox 或其他控件,并使用逻辑运算符(如 ANDOR)构建更复杂的 RowFilter 表达式。
  3. 对于数据量较大的情况,可以考虑使用异步筛选,避免 UI 阻塞,提高用户体验。
  4. 对用户输入进行验证和清理,防止 SQL 注入风险,例如对特殊字符进行转义处理。
private void textBoxFilter_TextChanged(object sender, EventArgs e)
{
    string filterText = textBoxFilter.Text;
    if (string.IsNullOrEmpty(filterText))
    {
        dataView.RowFilter = "";
    }
    else
    {
        // 对特殊字符进行转义处理
        filterText = filterText.Replace("'", "''");
        dataView.RowFilter = $"sampleName LIKE '%{filterText}%'";
    }
}

这样可以避免用户输入的文本中含有单引号等特殊字符时可能引发的异常。

  1. 可以添加一个按钮,让用户手动触发筛选操作,而不是在用户输入时实时筛选,这样可以避免频繁的筛选操作对性能的影响。
private void buttonFilter_Click(object sender, EventArgs e)
{
    string filterText = textBoxFilter.Text;
    if (string.IsNullOrEmpty(filterText))
    {
        dataView.RowFilter = "";
    }
    else
    {
        // 对特殊字符进行转义处理
        filterText = filterText.Replace("'", "''");
        dataView.RowFilter = $"sampleName LIKE '%{filterText}%'";
    }
}

将上述代码添加到一个按钮的 Click 事件处理程序中,用户点击按钮时进行筛选。

你可能感兴趣的:(前端技术,C#编程,前端)