我用它来查找 DataGridView 中的值:
private void fndBtn_Click(object sender, EventArgs e)
{
BindingSource src = new BindingSource();
src.DataSource = dataGridView1.DataSource;
src.Position = src.Find("p_Name", textBox1.Text);
}
但我有两个问题。首先,当我查找 dgv 中不存在的项目时,position 返回 0,默认情况下是第一行。我不希望这样,如果我使用 If 语句进行验证,我将丢失位置 0,从而丢失第一行。
第二是我希望关注行标题并突出显示找到的项目。这怎么可能?
最佳答案
像这样使用dataGridView的绑定(bind)源:
private void fndBtn_Click(object sender, EventArgs e)
{
BindingSource src = new BindingSource();
src.DataSource = dataGridView1.DataSource;
int findedRow = 0;
if (textBox1.Text!="")
findedRow = src.Find("p_Name", textBox1.Text);
if (findedRow!=-1)
src.Position = findedRow ;
}
关于c# - 有关 BindingSource 中 Find() 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2421090/