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

C# Windows Forms中通过两个窗体实现DataGridView筛选功能

问题描述

我有2个Windows Forms窗体,第一个窗体上放置了DataGridView控件,第二个窗体上仅配置了若干checkedListBox控件。
界面截图
当前已实现的逻辑为:在第二个窗体勾选选项后会自动保存选中的内容,但我需要将这些选中的选项传递到第一个窗体,用于筛选DataGridView中的数据。
问题:如何通过两个窗体实现筛选功能?

现有相关代码

第一个窗体代码

private void button2_Click_2(object sender, EventArgs e)
{
    Form2 form2 = new Form2(this);
    form2.InitializeSecondForm();
    form2.Show();
} 

public void InitializeSecondForm()
{
    this.Height = Properties.Settings.Default.SecondFormHeight;
    this.Width = Properties.Settings.Default.SecondFormWidth;
    this.Location = Properties.Settings.Default.SecondFormLocation;
    this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;

    this.FormClosing += SecondFormClosingEventHandler;
    this.StartPosition = FormStartPosition.Manual;
} 
private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
    Properties.Settings.Default.SecondFormHeight = this.Height;
    Properties.Settings.Default.SecondFormWidth = this.Width;
    Properties.Settings.Default.SecondFormLocation = this.Location;
    Properties.Settings.Default.DICOMTagSettings = this.collectionOfTags;

    Properties.Settings.Default.Save();
} 
private void fileListDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    DicomFileInfo dicomFile = new DicomFileInfo();
    DataGridView datas = new DataGridView();
    
    datas.DataSource = dicomFile;

    for(int i = 0; i < datas.Rows.Count; i++)
    {
        DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
        datas[2, i] = linkCell;
    }
} 

第二个窗体(Form2)代码

public partial class Form2 : Form
{
    StringCollection collectionOfTags = new StringCollection();
    public Form2()
    {
        InitializeComponent();
        InitializeSecondForm();
        LoadSettings();
    }

    private void LoadSettings()
    {
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            var d = checkedListBox1.Items[i];
            if (collectionOfTags.Contains(d.ToString()))
            {
                int index = checkedListBox1.Items.IndexOf(d);
                if (index != -1)
                    checkedListBox1.SetItemChecked(index, true);
            }
        }
        // 剩余checkedListBox2~6的加载逻辑同上
    }

    public void InitializeSecondForm()
    {
        this.Height = Properties.Settings.Default.SecondFormHeight;
        this.Width = Properties.Settings.Default.SecondFormWidth;
        this.Location = Properties.Settings.Default.SecondFormLocation;
        this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;

        this.FormClosing += SecondFormClosingEventHandler;
        this.StartPosition = FormStartPosition.Manual;
    }

    private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.SecondFormHeight = this.Height;
        Properties.Settings.Default.SecondFormWidth = this.Width;
        Properties.Settings.Default.SecondFormLocation = this.Location;
        Properties.Settings.Default.DICOMTagSettings = this.collectionOfTags;

        Properties.Settings.Default.Save();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Properties.Settings.Default.DICOMTagSettings.Clear();
        // 依次保存6个checkedListBox的选中项到配置
        this.Close();
    }
}
实现方案

你已经在打开Form2的时候传入了当前Form1的实例,直接基于这个实例传递参数即可,一共三步:

  1. 在Form1中新增公共筛选刷新方法,用来接收选中的标签参数并刷新DataGridView:
// Form1代码中新增
public void RefreshGridFilter(StringCollection selectedTags)
{
    // 此处根据你的实际数据源类型修改筛选逻辑,以下为绑定DataTable的示例
    if (fileListDataGridView.DataSource is not DataTable dt) return;

    if (selectedTags == null || selectedTags.Count == 0)
    {
        dt.DefaultView.RowFilter = string.Empty;
        return;
    }

    // 拼接筛选条件,将[标签列名]替换为你实际要匹配的DataGridView列名
    var filterConditions = selectedTags.Cast<string>()
        .Select(tag => $"[标签列名] = '{tag.Replace("'", "''")}'");
    dt.DefaultView.RowFilter = string.Join(" OR ", filterConditions);
}
  1. 修改Form2的构造函数,接收并保存Form1的实例:
public partial class Form2 : Form
{
    StringCollection collectionOfTags = new StringCollection();
    // 新增字段存储主窗体实例
    private Form1 _mainForm;

    // 替换原有无参构造函数
    public Form2(Form1 mainForm)
    {
        InitializeComponent();
        _mainForm = mainForm;
        InitializeSecondForm();
        LoadSettings();
    }
    // 原有其他代码保持不变
}
  1. 在Form2的确认按钮点击事件中,调用Form1的筛选方法,在button1_Click方法最后、this.Close()之前添加代码:
// 传入最新选中的标签,刷新主窗体表格
_mainForm.RefreshGridFilter(Properties.Settings.Default.DICOMTagSettings);

如果需要实时筛选(勾选选项立即生效不需要关闭窗体),可以给所有checkedListBox绑定ItemCheck事件,每次勾选变化时调用上述刷新方法即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 10:36:04