Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a datagrideveiw with about 35000 rows , and i want to filter it by a textbox. i wrot this code:
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;
    }
}

but its not working

What I have tried:

Al thing i think but not working
Posted
Updated 15-Jan-23 3:30am
Comments
Graeme_Grant 15-Jan-23 8:30am    
Are you using VirtualMode?
Houman Farokhi 15-Jan-23 8:42am    
Yes bu its not working
Graeme_Grant 15-Jan-23 9:51am    
Read my comment below to @0x01AA ... VirtualMode does not do what you (and I) thought. My understanding was based on WPF. View the links provided on how it works.

My solution is a tool that you can use to try out filters.

1 solution

Okay, I have a sample app to show you how to filter. It is based on this: BindingSource.Filter Property (System.Windows.Forms) | Microsoft Learn[^]

I have turned on VirtualMode and created 35,000 rows to reproduce your situation.

Form code-behind:
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;
}

NOTE:
* I am using a DataSet to hold the DataTable. This is optional and only required if you want to have multiple Tables.

Here is the auto-generated form (trimmed):
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;
}

Now you can enter search queries into the text search box and apply them by clicking on the button. For example, find all between ages 30 and 40:
Age > 30 and Age < 40

or a more complex query:
firstname like '%123%' and (age > 30 and age < 40)

Try different queries on the data. Maybe modify this test app with your own data and use it with different queries to see how the filter works.

I highly recommend creating a new project, dropping in my code, running it, and seeing how it works before making any changes to your code.
 
Share this answer
 
v4
Comments
0x01AA 15-Jan-23 9:36am    
Sorry for my stupid question: What means 'VirtualMode'?
Thanks and a 5.
Graeme_Grant 15-Jan-23 9:44am    
Hmmm... I don't normally do WinForm. Thought it was the same as WPF/Blazor/MAUI/Xamarin.

DataGridView.VirtualMode Property (System.Windows.Forms) | Microsoft Learn[^].

Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.

More information here on bound and unbound: Virtual Mode in DataGridView Control - Windows Forms .NET Framework | Microsoft Learn[^]

ps: thanks for the vote! 😊
0x01AA 15-Jan-23 9:50am    
Thank you Sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900