如果第 0 列中的单元格包含指定值,我需要选择整行。我有一个 TextBox 和 DaraGridView。当我把一个值存在于 gridview 行中时选择没有问题但是当一个 put 在 gridview 中不存在时,程序会抛出一个异常提前谢谢你!
private void Searchvalue_TextChanged(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
var enteredText = (sender as TextBox).Text;
int rowIndex = -1;
bool tempAllowUserToAddRows = dataGridView1.AllowUserToAddRows;
// Turn off or .Value below will throw null exception
dataGridView1.AllowUserToAddRows = false;
if (enteredText != String.Empty)
{
DataGridViewRow row = dataGridView1.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["Column1"].Value.ToString().Contains(enteredText)).First();
rowIndex = row.Index;
dataGridView1.AllowUserToAddRows = tempAllowUserToAddRows;
dataGridView1.Rows[rowIndex].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[rowIndex].Index;
}
}