在选定索引处选择 DataGridViewRow

发布于 2024-12-20 23:51:59 字数 645 浏览 4 评论 0原文

我在搜索时从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-12-27 23:51:59

您可以使用 CurrentCellDataGridView 的 > 属性来设置选择。如果您使用 FullRowSelect 作为选择模式,只需使用您想要选择的行中的任何单元格即可。
例如:

...
if (tempCode == code_tb.Text) //Checks if code matchs the search code
{ 
     products_dgv.CurrentCell = row.Cells[0];
     break;
}
...

You can use the CurrentCell property of the DataGridView to set the selection. If you are using FullRowSelect as selection mode, just use any cells in the row that you would like to select.
For example:

...
if (tempCode == code_tb.Text) //Checks if code matchs the search code
{ 
     products_dgv.CurrentCell = row.Cells[0];
     break;
}
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文