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:
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):
#region Windows Form Designer generated code
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();
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;
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;
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);
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.