private void textBox2_TextChanged(object sender, EventArgs e)
{
try
{
this.dataGridView1.ClearSelection();
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
if (r.Cells[1].Value != null)
{
if ((r.Cells[1].Value).ToString().StartsWith(this.textBox2.Text.Trim()))
{
this.dataGridView1.Rows[r.Index].Selected = true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.ClearSelection();
}
答案 0 :(得分:0)
Yorye Nathan的评论是正确的
try
{
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
if (r.Cells[0].Value != null)
{
if ((r.Cells[0].Value).ToString().StartsWith(this.textBox1.Text.Trim()) && textBox1.Text.Trim().Length > 0)
{
this.dataGridView1.Rows[r.Index].Visible = false;
}
else
{
this.dataGridView1.Rows[r.Index].Visible = true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
假设输入类似于
this.dataGridView1.Columns.Add("Text", "Text");
this.dataGridView1.Rows.Add("five");
this.dataGridView1.Rows.Add("six");
this.dataGridView1.Rows.Add("seven");
this.dataGridView1.Rows.Add("eight");
this.dataGridView1.Rows.Add("nine");