【解決方法】bindingsource で datagrideview をフィルタリングする

プログラミングQA


私は約 35000 行のデータグリッドを持っており、テキスト ボックスでフィルター処理したいと考えています。 私はこのコードを書きました:

C#
void txtSearch_EditValueChanged(object sender, EventArgs e)
{
    BindingSource firstBs = new();
    firstBs.DataSource = dgvIcd11.DataSource;
    if (txtSearch.Text.Trim() != string.Empty)
    {
        BindingSource bs = new();
        bs.DataSource = dgvIcd11.DataSource;
        bs.Filter = string.Format(
                                "FORMALTITLE like '%{0}%' or INFORMALTITLE like '%{0}%'  or  CODE like '%{0}%'",
                                arg0: txtSearch.Text);
        MessageBox.Show(bs.Filter);
        dgvIcd11.DataSource = bs;
    }
    else
    {
        dgvIcd11.DataSource = firstBs;
    }
}

しかし、それは機能していません

私が試したこと:

私が思うがうまくいかないこと

解決策 1

わかりました。フィルタリングの方法を示すサンプル アプリがあります。 それはこれに基づいています: BindingSource.Filter プロパティ (System.Windows.Forms) | マイクロソフト ラーン[^]

私は持っている オン VirtualMode 状況を再現するために 35,000 行を作成しました。

フォームの分離コード:

C#
public partial class Form1 : Form
{
    DataSet dataSet = new();
    BindingSource bindingSource = new();

    private Random random = new();

    public Form1()
    {
        InitializeComponent();
        InitData();
    }

    private void InitData()
    {
        DataTable dataTable = new();

        dataTable.Columns.Add(new DataColumn("FirstName"));
        dataTable.Columns.Add(new DataColumn("LastName"));
        dataTable.Columns.Add(new DataColumn("Age"));

        for (int i = 0; i < 35000; i++)
        {
            var row = dataTable.NewRow();
            row["FirstName"] = $"FirstName{i:000#}";
            row["LastName"] = $"LastName{i:000#}";
            row["Age"] = random.Next(20, 60);
            dataTable.Rows.Add(row);
        }

        dataSet.Tables.Add(dataTable);

        DataView view1 = new DataView(dataSet.Tables[0]);
        bindingSource.DataSource = view1;

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = bindingSource;
    }

    private void butSearch_Click(object sender, EventArgs e)
        => bindingSource.Filter = txtSearch.Text;
}

ノート:
*私は DataSet を保持する DataTable. これはオプションで、複数のテーブルが必要な場合にのみ必要です。

自動生成されたフォーム (トリミング) は次のとおりです。

C#
#region Windows Form Designer generated code

/// <summary>
///  Required method for Designer support - do not modify
///  the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.txtSearch = new System.Windows.Forms.TextBox();
    this.butSearch = new System.Windows.Forms.Button();
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGridView1
    // 
    this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Top;
    this.dataGridView1.Location = new System.Drawing.Point(0, 0);
    this.dataGridView1.Name = "dataGridView1";
    this.dataGridView1.RowHeadersWidth = 62;
    this.dataGridView1.RowTemplate.Height = 33;
    this.dataGridView1.Size = new System.Drawing.Size(800, 419);
    this.dataGridView1.TabIndex = 0;
    // 
    // txtSearch
    // 
    this.txtSearch.Dock = System.Windows.Forms.DockStyle.Left;
    this.txtSearch.Location = new System.Drawing.Point(0, 419);
    this.txtSearch.Name = "txtSearch";
    this.txtSearch.Size = new System.Drawing.Size(682, 31);
    this.txtSearch.TabIndex = 2;
    // 
    // butSearch
    // 
    this.butSearch.Dock = System.Windows.Forms.DockStyle.Right;
    this.butSearch.Location = new System.Drawing.Point(688, 419);
    this.butSearch.Name = "butSearch";
    this.butSearch.Size = new System.Drawing.Size(112, 31);
    this.butSearch.TabIndex = 3;
    this.butSearch.Text = "button1";
    this.butSearch.UseVisualStyleBackColor = true;
    this.butSearch.Click += new System.EventHandler(this.butSearch_Click);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(800, 450);
    this.Controls.Add(this.butSearch);
    this.Controls.Add(this.txtSearch);
    this.Controls.Add(this.dataGridView1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    this.ResumeLayout(false);
    this.PerformLayout();

}

#endregion

private DataGridView dataGridView1;
private TextBox txtSearch;
private Button butSearch;
}

テキスト検索ボックスに検索クエリを入力し、ボタンをクリックして適用できるようになりました。 たとえば、30 歳から 40 歳までのすべてを検索します。

Age > 30 and Age < 40

またはより複雑なクエリ:

firstname like '%123%' and (age > 30 and age < 40)

データに対してさまざまなクエリを試してください。 このテスト アプリを独自のデータで変更し、さまざまなクエリで使用して、フィルターがどのように機能するかを確認してください。

コードに変更を加える前に、新しいプロジェクトを作成し、コードをドロップして実行し、それがどのように機能するかを確認することを強くお勧めします。

コメント

タイトルとURLをコピーしました