我想DataGridView
连续查找字符串的索引,这是我的代码:
DataGridViewRow row = dgvVisual.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => (string)r.Cells[0].Value == "9");
但是返回值row
是null
a而不是a DataGridViewRow
,怎么解决这个问题呢?
整个代码在这里:
SqlCommand cmd;
cmd = new SqlCommand("select * from MyTable , SqlConnection);
SqlDataReader read;
read = cmd.ExecuteReader();
while (read.Read())
{
int ColIndex= int.Parse(read["ColumnName"].ToString()) + 1;
int RowIndex = -1;
DataGridViewRow row = dgvVisual.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => (string)r.Cells[0].Value == (string)read["ColumnNameB"].ToString().Trim());
if (row != null)
RowIndex = row.Index;
DataGridViewCheckBoxCell chkbxCell = (DataGridViewCheckBoxCell)dgvVisual[ColIndex, RowIndex];
chkbxCell.Value = true;
}
read.Close();
问题已解决,因为数据库中存在一些问题并且回读不正确。