DataGridView-如何跳转到搜索中的选定行? [英] DataGridView- How to jump to the selected row in search?

查看:284
本文介绍了DataGridView-如何跳转到搜索中的选定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在DataGridView中找到一行,并突出显示该行。

I use the following code to find a row in DataGridView and highlight the row.

private void btnSearch_Click(object sender, EventArgs e)
    {
        currentMode = ModeSelection.Search;

        if (cmbSearchBy.SelectedIndex == Convert.ToInt16(SearchBy.MaterialID))
        {
            dgvSearchResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            int rowIndex = -1;
            try
            {
                foreach (DataGridViewRow row in dgvSearchResults.Rows)
                {
                    if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
                    {
                        //Select the row here
                        rowIndex = row.Index;
                        dgvSearchResults.Rows[rowIndex].Selected = true;
                        break;
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }

它工作得很好。问题是我的DataGridView有超过500条记录,如果所选行靠近DataGridView的底部,用户必须将其滚动到底部。我可以使用哪个代码来跳转到我正在寻找的行?任何帮助将非常感谢!

It works perfectly. Problem is my DataGridView has over 500 records and if the selected row is near the bottom of the DataGridView, users have to scroll all the way down to the bottom. Which code can I use to jump to the row that I am looking for? Any help will be very much appreciated!

推荐答案

我发现我可以使用 DataGridView.FirstDisplayedScrollingRowIndex 滚动到所选行索引的属性,并将其显示为DataGridView中的第一行。

I found out that I can use DataGridView.FirstDisplayedScrollingRowIndex Property to scroll to the selected row index and display it as the first row in DataGridView.

这是我在我的程序中使用的 - p>

This is how I used in my program-

if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
    rowIndex = row.Index;
    dgvSearchResults.ClearSelection();                            
    dgvSearchResults.Rows[rowIndex].Selected = true;
    dgvSearchResults.FirstDisplayedScrollingRowIndex = rowIndex;
    dgvSearchResults.Focus();
    break;
}

这篇关于DataGridView-如何跳转到搜索中的选定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆