如何让DataGridView显示所选行?

时间:2011-12-08 21:26:54

标签: c# winforms datagridview scroll selected

我需要强制DataGridView显示所选的row

简而言之,我有textbox根据DGV中输入的内容更改textbox选项。发生这种情况时,选择会更改为匹配的row

不幸的是,如果选定的row不在视图中,我必须手动向下滚动才能找到选择。有谁知道如何强制DGV显示所选的row

谢谢!

9 个答案:

答案 0 :(得分:113)

您可以设置:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;

这是此属性的MSDN documentation

答案 1 :(得分:45)

这个滚动到选定的行而不将其放在顶部。

dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];

答案 2 :(得分:20)

还要考虑这段代码(使用来自competent_tech建议的方式):

private static void EnsureVisibleRow(DataGridView view, int rowToShow)
{
    if (rowToShow >= 0 && rowToShow < view.RowCount)
    {
        var countVisible = view.DisplayedRowCount(false);
        var firstVisible = view.FirstDisplayedScrollingRowIndex;
        if (rowToShow < firstVisible)
        {
            view.FirstDisplayedScrollingRowIndex = rowToShow;
        }
        else if (rowToShow >= firstVisible + countVisible)
        {
            view.FirstDisplayedScrollingRowIndex = rowToShow - countVisible + 1;
        }
    }
}

答案 3 :(得分:10)

在选择行之后放置该行:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;

答案 4 :(得分:1)

int rowIndex = -1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.Cells[0].Value.ToString().Equals(searchString))
    {
        rowIndex = row.Index;
        break;
    }
}
if (rowIndex >= 0)
{
    dataGridView1.CurrentCell = dataGridView1[visibleColumnIndex, rowIndex];
}

visibleColumnIndex - 所选单元格必须可见

答案 5 :(得分:1)

请注意,在未启用DataGridView时设置 FirstDisplayedScrollingRowIndex 会将列表滚动到所需的行,但滚动条不会反映其位置。最简单的解决方案是重新启用和禁用您的DGV。

dataGridView1.Enabled = true;
dataGridView1.FirstDisplayedScrollingRowIndex = index;
dataGridView1.Enabled = false;

答案 6 :(得分:1)

//这有效,它区分大小写并找到第一次出现的搜索

    private bool FindInGrid(string search)
    {
        bool results = false;

        foreach (DataGridViewRow row in dgvData.Rows)
        {
            if (row.DataBoundItem != null)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Value.ToString().Contains(search))
                    {
                        dgvData.CurrentCell = cell;
                        dgvData.FirstDisplayedScrollingRowIndex = cell.RowIndex;
                        results = true;
                        break;
                    }

                    if (results == true)
                        break;
                }
                if (results == true)
                    break;
            }
        }

        return results;
    }

答案 7 :(得分:0)

做这样的事情:

dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];

仅在第一列可见时才有效。如果它被隐藏,你会得到一个例外。这更安全:

var column = dataGridView1.CurrentCell != null ? dataGridView1.CurrentCell.ColumnIndex : dataGridView1.FirstDisplayedScrollingColumnIndex; dataGridView1.CurrentCell = dataGridView1.Rows[iNextHighlight].Cells[column];

如果目标行已经在屏幕上,这将重置选择而不滚动。它还保留了当前列选择,这在您允许内联编辑的情况下很重要。

答案 8 :(得分:0)

我制作了下一个搜索功能,以便在显示中滚动选择。

private void btnSearch_Click(object sender, EventArgs e)
{
  dataGridView1.ClearSelection();
  string strSearch = txtSearch.Text.ToUpper();
  int iIndex = -1;
  int iFirstFoundRow = -1;
  bool bFound = false;
  if (strSearch != "")
  {
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

    /*  Select All Rows Starting With The Search string in row.cells[1] =
    second column. The search string can be 1 letter till a complete line
    If The dataGridView MultiSelect is set to true this will highlight 
    all found rows. If The dataGridView MultiSelect is set to false only 
    the last found row will be highlighted. Or if you jump out of the  
    foreach loop the first found row will be highlighted.*/

   foreach (DataGridViewRow row in dataGridView1.Rows)
   {
     if ((row.Cells[1].Value.ToString().ToUpper()).IndexOf(strSearch) == 0)
     {
       iIndex = row.Index;
       if(iFirstFoundRow == -1)  // First row index saved in iFirstFoundRow
       {
         iFirstFoundRow = iIndex;
       }
       dataGridView1.Rows[iIndex].Selected = true; // Found row is selected
       bFound = true; // This is needed to scroll de found rows in display
       // break; //uncomment this if you only want the first found row.
     }
   }
   if (bFound == false)
   {
     dataGridView1.ClearSelection(); // Nothing found clear all Highlights.
   }
   else
   {
     // Scroll found rows in display
     dataGridView1.FirstDisplayedScrollingRowIndex = iFirstFoundRow; 
   }
}

}