查找基于列的和价值的dataGridView行 [英] Find a row in dataGridView based on column and value
问题描述
我有一个有3列一个DataGridView:SYSTEMID,名字,姓氏正在使用的数据库信息的约束。我想强调一定行,我会做使用:
I have a dataGridView that has 3 columns: SystemId, FirstName, LastName that is bound using database information. I would like to highlight a certain row, which I would do using:
dataGridView1.Rows[????].Selected = true;
行ID然而,我不知道和BindingSource的不断变化,因此第10行可能是约翰·史密斯的一个实例,但即使在另一个存在(我有一个过滤器,基于进入哪些用户过滤掉源,因此键入[约将产生的所有行姓/名有[约在其中,从而我的名单可以从50名到3点击)。
The row ID I however do not know and the bindingsource keeps changing, thus row 10 could be "John Smith" in one instance but not even exist in another (I have a filter that filters out the source based on what user enters, so typing in "joh" would yield all rows where first / last name have "joh" in them, thus my list can go from 50 names to 3 in a click).
我想找到一种方法,我怎么能选择基于SYSTEMID和相应数量的行。我可以用下面的方法来获取系统ID:
I want to find a way how I can select a row based on SystemId and a corresponding number. I can get the system ID using the following method:
systemId = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SystemId"].Value.ToString();
现在我只是需要将其应用到该行选择器。像dataGridView1.Columns [SYSTEMID]。的IndexOf(的systenId}但这不起作用(也没有这样的方法存在的话)。任何帮助是极大的赞赏。
Now I just need to apply it to the row selector. Something like dataGridView1.Columns["SystemId"].IndexOf(systemId} but that does not work (nor does such method exist). Any help is greatly appreciated.
推荐答案
这会给你的价值在GridView行索引:
This will give you the gridview row index for the value:
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;
这篇关于查找基于列的和价值的dataGridView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!