我希望我的DataGridView在我输入时过滤数据。
private void textSearch_TextChanged(object sender, EventArgs e)
{
Console.WriteLine("text_search_TextChanged()");
LoadToDataGridView();
filterTextBox.Focus();
}
这段代码很慢。当我输入时,它会在每个字符处停止。
我可以做些什么来提高速度?
源代码
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";
}
}