I have windows form app using C#. I need to do incremental search inside the grid control from a text box. The problem when I type few letters the app hangup for 6 seconds until it display what I wrote and filter the grid. Currently there are 3 million records. Is there a way to make incremental search faster ?
I use this code to filter the result in a grid:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string searchValue = textBox1.Text.Trim().ToLower();
if (string.IsNullOrEmpty(searchValue))
{
dataGrid1.DataSource = dataTable;
}
else
{
var filteredRows = dataTable.AsEnumerable()
.Where(row => row.ItemArray.Any(
field => field.ToString().ToLower().Contains(searchValue)));
if (filteredRows.Any())
{
dataGrid1.DataSource = filteredRows.CopyToDataTable();
}
else
{
dataGrid1.DataSource = dataTable.Clone(); // No matches, clear grid
}
}
}