我需要强制DataGridView
显示所选内容row
。
简而言之,我有一个textbox
可DGV
根据键入的内容更改选择textbox
。发生这种情况时,选择将更改为match row
。
不幸的是,如果所选row
内容不在视图中,我必须手动向下滚动才能找到所选内容。有谁知道如何强制DGV
显示所选内容row
?
谢谢!
Answers:
您可以设置:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
这是有关此属性的MSDN文档。
此滚动条滚动到所选行,而不放在最上面。
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
DataGridView.FirstDisplayedScrollingRowIndex
,谢谢!
dataGridView1.CurrentCell = dataGridView1.SelectedRows[0].Cells[0]
还考虑以下代码(使用来自于主管技术建议的方式):
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;
}
}
}
else if (rowToShow >= firstVisible + countVisible - 1)
我写了相同的解决方案,因为我错过了此答复!)
只需在选择该行之后放置该行:
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-所选单元格必须可见
//可以区分大小写并查找第一次出现的搜索
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;
}
做这样的事情:
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
仅在第一列可见时才起作用。如果它是隐藏的,您将得到一个例外。这更安全:
var column = dataGridView1.CurrentCell != null ?
dataGridView1.CurrentCell.ColumnIndex :
dataGridView1.FirstDisplayedScrollingColumnIndex;
dataGridView1.CurrentCell = dataGridView1.Rows[iNextHighlight].Cells[column];
如果目标行已经在屏幕上,这将重置选择而不滚动。它还保留了当前列的选择,这在允许内联编辑的情况下可能很重要。
我做了下一个搜索功能,该功能可用于滚动显示中的选择。
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;
}
}
}