【发布时间】:2018-05-29 19:17:51
【问题描述】:
如果第 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;
}
}
【问题讨论】:
-
"抛出异常"....我们怎么可能在不知道更多信息的情况下帮助您?什么是异常类型?消息是什么?
-
[链接] ibb.co/fiTGLy
-
附加信息:序列不包含任何元素。
标签: c# exception datagridview selecteditem datagridviewrow