I want my DataGridView filters data as I type.
private void textSearch_TextChanged(object sender, EventArgs e)
{
Console.WriteLine("text_search_TextChanged()");
LoadToDataGridView();
filterTextBox.Focus();
}
This code is painfully slow. It halts at each character as I type.
What can I do to improve the speed?
.
Source code
void LoadToDataGridView()
{
Console.WriteLine("LoadToDataGridView()...");
dataGridView1.Rows.Clear();
IList<Word> items = null;
string like = filterTextBox.Text;
if (groupCheckBox.Checked || learntCheckBox.Checked)
{
LearntTypeEnum learntType = (LearntTypeEnum)learntComboBox.SelectedIndex;
int groupNo = (int)groupComboBox.Items[groupComboBox.SelectedIndex];
int correctness = InMemoryValues.CorrectnessRepetition;
if(groupCheckBox.Checked && learntCheckBox.Checked)
{
if (learntType == LearntTypeEnum.All)//all
{
items = _wordDatabase.GetByGroup(groupNo);//, like);
}
else if (learntType == LearntTypeEnum.NotLearnt)//not learnt
{
items = _wordDatabase.GetByCorrectnessBelow(groupNo, correctness);//, like);
}
else//learnt
{
items = _wordDatabase.GetByCorrectnessBeyond(groupNo, correctness);//, like);
}
}
else if (groupCheckBox.Checked)
{
items = _wordDatabase.GetByGroup(groupNo);//, like);
}
else // if(learntCheckBox.Checked)
{
if (learntType == LearntTypeEnum.All)//all
{
items = _wordDatabase.Get();//ByLike(like);
}
else if (learntType == LearntTypeEnum.NotLearnt)//not learnt
{
items = _wordDatabase.GetByCorrectnessBelow(correctness);//, like);
}
else//learnt
{
items = _wordDatabase.GetByCorrectnessBeyond(correctness);//, like);
}
}
}
else
{
items = _wordDatabase.Get();
}
if (items != null)
{
if (items.Count > 0)
{
Console.WriteLine(items[0].Name);
int i = 0;
foreach (Word c in items)
{
dataGridView1.Rows.Add((c.CorrectnessCount==InMemoryValues.CorrectnessRepetition)?true:false
,c.GroupNo, c.Name, c.CorrectnessCount, c.Meaning);
dataGridView1.Rows[i].Tag = c;
++i;
}
IList<Word> likes = _wordDatabase.GetByLike(like);
if(likes != null)
{
if(likes.Count > 0)
{
Word word = likes[0];
SetFocusToWord(word);
}
}
totalRowsLabel.Text = items.Count.ToString();
}
else
{
totalRowsLabel.Text = "0";
}
}
else
{
totalRowsLabel.Text = "0";
}
}