How do I make the DataGridView show the selected r

2020-02-02 06:16发布

I need to force the DataGridView to show the selected row.

In short, I have a textbox that changes the DGV selection based on what is typed into the textbox. When this happens, the selection changes to the matching row.

Unfortunately if the selected row is out of the view, I have to manually scroll down to find the selection. Does anyone know how to force the DGV to show the selected row?

Thanks!

9条回答
老娘就宠你
2楼-- · 2020-02-02 06:36

You can set:

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

Here is the MSDN documentation on this property.

查看更多
聊天终结者
3楼-- · 2020-02-02 06:36

Just put that line after the selecting the row:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-02 06:40

Doing something like this:

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

will only work if the first column is visible. If it is hidden, you'll get an exception. This is safer:

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

This will reset the selection without scrolling if the target row is already on screen. It also preserves the current column choice which can matter in cases where you've allowed inline editing.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-02 06:42

I made the next search function it works wel for scrolling selections in display.

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; 
   }
}

}

查看更多
小情绪 Triste *
6楼-- · 2020-02-02 06:49

Consider also this code (uses the from competent_tech suggested way):

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;
        }
    }
}
查看更多
家丑人穷心不美
7楼-- · 2020-02-02 06:49
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 - selected cell must be visible

查看更多
登录 后发表回答