根据列和值在dataGridView中查找一行

我有一个dataGridView有3列:SystemId,FirstName,LastName绑定使用数据库信息。 我想突出显示某个行,我将使用:

dataGridView1.Rows[????].Selected = true; 

然而,行ID我不知道,绑定源不断变化,因此第10行可能是“约翰史密斯”在一个实例,但不存在于另一个(我有一个filter,根据用户input过滤掉源,所以键入在“joh”将产生所有行中的名/姓“joh”,因此我的列表可以从50个名字到3点击)。

我想find一种方法,我可以select一个基于SystemId和相应的数字的行。 我可以使用以下方法获得系统ID:

 systemId = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SystemId"].Value.ToString(); 

现在我只需要将其应用到行select器。 像dataGridView1.Columns [“SystemId”]。IndexOf(systemId),但不起作用(也不存在这样的方法)任何帮助,非常感谢。

这会给你的gridview行索引值:

 String searchValue = "somestring"; int rowIndex = -1; foreach(DataGridViewRow row in DataGridView1.Rows) { if(row.Cells[1].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; break; } } 

或者一个LINQ查询

 int rowIndex = -1; DataGridViewRow row = dgv.Rows .Cast<DataGridViewRow>() .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue)) .First(); rowIndex = row.Index; 

那么你可以这样做:

 dataGridView1.Rows[rowIndex].Selected = true; 

上面的答案只有在AllowUserToAddRows设置为false时才有效。 如果该属性设置为true ,那么当循环或Linq查询尝试协商新行时,您将得到一个NullReferenceException 。 我修改了上面两个接受的答案来处理AllowUserToAddRows = true

循环回答:

 String searchValue = "somestring"; int rowIndex = -1; foreach(DataGridViewRow row in DataGridView1.Rows) { if (row.Cells["SystemId"].Value != null) // Need to check for null if new row is exposed { if(row.Cells["SystemId"].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; break; } } } 

LINQ回答:

 int rowIndex = -1; bool tempAllowUserToAddRows = dgv.AllowUserToAddRows; dgv.AllowUserToAddRows = false; // Turn off or .Value below will throw null exception DataGridViewRow row = dgv.Rows .Cast<DataGridViewRow>() .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue)) .First(); rowIndex = row.Index; dgv.AllowUserToAddRows = tempAllowUserToAddRows; 

或者你可以这样使用。 这可能会更快。

 int iFindNo = 14; int j = dataGridView1.Rows.Count-1; int iRowIndex = -1; for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count/2) +1; i++) { if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == iFindNo) { iRowIndex = i; break; } if (Convert.ToInt32(dataGridView1.Rows[j].Cells[0].Value) == iFindNo) { iRowIndex = j; break; } j--; } if (iRowIndex != -1) MessageBox.Show("Index is " + iRowIndex.ToString()); else MessageBox.Show("Index not found." ); 

尝试这个:

  string searchValue = textBox3.Text; int rowIndex = -1; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; try { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["peseneli"].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0]; dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true; break; } } } catch (Exception exc) { MessageBox.Show(exc.Message); } 

如果你只是想检查该项目是否存在:

 IEnumerable<DataGridViewRow> rows = grdPdfs.Rows .Cast<DataGridViewRow>() .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue)); if (rows.Count() == 0) { // Not Found } else { // Found } 

这是build立在戈登的上述答案之上的 – 并不是所有这些都是我原来的工作。 我所做的是添加一个更通用的方法,我的静态工具类。

 public static int MatchingRowIndex(DataGridView dgv, string columnName, string searchValue) { int rowIndex = -1; bool tempAllowUserToAddRows = dgv.AllowUserToAddRows; dgv.AllowUserToAddRows = false; // Turn off or .Value below will throw null exception if (dgv.Rows.Count > 0 && dgv.Columns.Count > 0 && dgv.Columns[columnName] != null) { DataGridViewRow row = dgv.Rows .Cast<DataGridViewRow>() .FirstOrDefault(r => r.Cells[columnName].Value.ToString().Equals(searchValue)); rowIndex = row.Index; } dgv.AllowUserToAddRows = tempAllowUserToAddRows; return rowIndex; } 

然后以任何forms我想要使用它,我调用传递DataGridView,列名称和search值的方法。 为了简单起见,我将所有内容都转换为searchstring,尽pipe添加指定数据types的重载会很容易。

 private void UndeleteSectionInGrid(string sectionLetter) { int sectionRowIndex = UtilityMethods.MatchingRowIndex(dgvSections, "SectionLetter", sectionLetter); dgvSections.Rows[sectionRowIndex].Cells["DeleteSection"].Value = false; } 

那些使用WPF的人

 for (int i = 0; i < dataGridName.Items.Count; i++) { string cellValue= ((DataRowView)dataGridName.Items[i]).Row["columnName"].ToString(); if (cellValue.Equals("Search_string")) // check the search_string is present in the row of ColumnName { object item = dataGridName.Items[i]; dataGridName.SelectedItem = item; // selecting the row of dataGridName dataGridName.ScrollIntoView(item); break; } } 

如果您想在此之后获取选定的行项目,则以下代码段会很有帮助

 DataRowView drv = dataGridName.SelectedItem as DataRowView; DataRow dr = drv.Row; string item1= Convert.ToString(dr.ItemArray[0]);// get the first column value from selected row string item2= Convert.ToString(dr.ItemArray[1]);// get the second column value from selected row