How to get a DataGridView to display the selected row?

I need to get the DataGridView show the selected row .

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

Unfortunately, if the selected row missing, I need to manually scroll down to find the selection. Does anyone know how to get DGV show the selected row ?

Thank!

+45
c # selected scroll winforms datagridview
Dec 08 '11 at
source share
8 answers

You can install:

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

Here is the MSDN documentation for this property.

+82
Dec 08 '11 at 9:32 a.m.
source share

This scrolls to the selected row without placing it on top.

 dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0]; 
+30
Jan 21 '13 at 14:48
source share

Consider also this code (uses the proposed method from 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; } } } 
+13
02 Feb '16 at 6:48
source share

Just put this line after selecting the line:

 dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index; 
+10
Dec 08 2018-11-12T00:
source share
 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 - the selected cell must be visible

+1
Nov 26 '14 at 13:05
source share

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 will get an exception. It 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 line is already on the screen. It also saves the current selection of columns, which may make a difference when you enable inline editing.

0
May 27 '15 at 13:40
source share

I made the following search function, which works well for scrolling selected on the 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; } } 

}

0
Sep 06 '15 at 8:39
source share

Note that setting FirstDisplayedScrollingRowIndex when your DataGridView is not turned on scrolls the list to the desired row, but the scroll bar does not reflect its position. The easiest solution is to re-enable and disable your DGV.

 dataGridView1.Enabled = true; dataGridView1.FirstDisplayedScrollingRowIndex = index; dataGridView1.Enabled = false; 
0
Dec 23 '16 at 11:45
source share



All Articles