Comment faire pour que le DataGridView affiche la ligne sélectionnée?

Je dois forcer le DataGridView pour afficher la row sélectionnée.

En bref, j’ai une textbox qui modifie la sélection DGV fonction de ce qui est tapé dans la textbox . Lorsque cela se produit, la sélection passe à la row correspondante.

Malheureusement, si la row sélectionnée est hors de vue, je dois faire défiler manuellement pour trouver la sélection. Est-ce que quelqu’un sait comment forcer le DGV à afficher la row sélectionnée?

Merci!

Vous pouvez définir:

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

Voici la documentation MSDN sur cette propriété.

Celui-ci défile sur la ligne sélectionnée sans la mettre en haut.

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

Considérez aussi ce code (utilise la méthode suggérée 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; } } } 

Il suffit de mettre cette ligne après la sélection de la ligne:

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

visibleColumnIndex – la cellule sélectionnée doit être visible

Faire quelque chose comme ça:

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

ne fonctionnera que si la première colonne est visible. S’il est caché, vous obtiendrez une exception. C’est plus sûr:

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

Cela réinitialisera la sélection sans faire défiler si la ligne cible est déjà à l’écran. Il conserve également le choix de colonne en cours, ce qui peut être important dans les cas où vous avez autorisé la modification en ligne.

J’ai effectué la prochaine fonction de recherche qui fonctionne pour faire défiler les sélections affichées.

 private void btnSearch_Click(object sender, EventArgs e) { dataGridView1.ClearSelection(); ssortingng 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 ssortingng in row.cells[1] = second column. The search ssortingng 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.ToSsortingng().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; } } 

}

Notez que la définition de FirstDisplayedScrollingRowIndex lorsque votre DataGridView n’est pas activé fait défiler la liste jusqu’à la ligne souhaitée, mais la barre de défilement ne reflète pas sa position. La solution la plus simple consiste à réactiver et désactiver votre DGV.

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

// Cela fonctionne, il est sensible à la casse et trouve la première occurrence de la recherche

  private bool FindInGrid(ssortingng search) { bool results = false; foreach (DataGridViewRow row in dgvData.Rows) { if (row.DataBoundItem != null) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value.ToSsortingng().Contains(search)) { dgvData.CurrentCell = cell; dgvData.FirstDisplayedScrollingRowIndex = cell.RowIndex; results = true; break; } if (results == true) break; } if (results == true) break; } } return results; }