在选定索引处选择 DataGridViewRow
我在搜索时从 DataGridView 中选择一行时遇到问题。数据源是来自数据库的DataTable。我正在使用一个搜索框来检查 DataGridView 中是否有与产品密钥匹配的产品,如果找到,我想选择它。 这就是我所拥有的:
private void search_btn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in products_dgv.Rows)
{
string tempCode = row.Cells[0].Value.ToString(); //Code comparing
if (tempCode == code_tb.Text) //Checks if code matchs the search code
{
//I would like to do a products_dgv.selectedIndex = row.Index but it
//doesnt work
break;
}
}
}
非常感谢任何帮助。谢谢你!
I am having problems selecting a row out of the DataGridView on a search. The data source is a DataTable from a database. I am using a search box that checks the DataGridView for a product matching the product key, and i want to select it if found.
Here is what i have:
private void search_btn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in products_dgv.Rows)
{
string tempCode = row.Cells[0].Value.ToString(); //Code comparing
if (tempCode == code_tb.Text) //Checks if code matchs the search code
{
//I would like to do a products_dgv.selectedIndex = row.Index but it
//doesnt work
break;
}
}
}
Any help is much appreciated. Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
CurrentCell
DataGridView 的 > 属性来设置选择。如果您使用FullRowSelect
作为选择模式,只需使用您想要选择的行中的任何单元格即可。例如:
You can use the
CurrentCell
property of theDataGridView
to set the selection. If you are usingFullRowSelect
as selection mode, just use any cells in the row that you would like to select.For example: