如何使DataGridView显示选定的行?

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

总之,我有一个textbox ,根据input到textbox textbox来更改DGVselect。 发生这种情况时,select将更改为匹配的row

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

谢谢!

你可以设置:

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

这是该属性的MSDN文档 。

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

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

也考虑这个代码(使用from competent_techbuild议的方式):

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

只需在select行之后放置该行:

 dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index; 
 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 – 选中的单元格必须可见

做这样的事情:

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

只有在第一列可见的情况下才有效。 如果它隐藏,你会得到一个例外。 这是更安全的:

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

如果目标行已经在屏幕上,这将重置没有滚动的select。 它还保留当前的列select,这可以在您允许内联编辑的情况下发生。

我做了下一个searchfunction,以便在显示中滚动select。

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

}

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

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