我需要强制DataGridView
显示选定的row
。如何使DataGridView显示选定的行?
总之,我有一个textbox
,根据输入到textbox
的内容更改DGV
选择。发生这种情况时,选择将更改为匹配row
。
不幸的是,如果选择的row
不在视图中,我必须手动向下滚动才能找到选择。有谁知道如何强制DGV
显示选定的row
?
谢谢!
我需要强制DataGridView
显示选定的row
。如何使DataGridView显示选定的行?
总之,我有一个textbox
,根据输入到textbox
的内容更改DGV
选择。发生这种情况时,选择将更改为匹配row
。
不幸的是,如果选择的row
不在视图中,我必须手动向下滚动才能找到选择。有谁知道如何强制DGV
显示选定的row
?
谢谢!
您可以设置:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
下面是这个属性的MSDN documentation。
谢谢!我一直在努力寻找我的逻辑错误,因为我使用的CurrentCell并不普遍。但是我可以将我一直使用的行号插入到这里,它的功能就像一个魅力! – clweeks
简单和完美(y) –
只要把该行的选择行后:
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];
这将重置选择不滚动,如果目标行已经在屏幕上。它还保留当前列选择,这在您允许内联编辑的情况下很重要。
我做了下一个搜索功能,它适用于在显示中滚动选择。
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;
}
}
}
这是我使用的一个。 – Mac
也考虑这个代码(使用从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;
}
}
}
请注意,设置FirstDisplayedScrollingRowIndex当你的DataGridView未启用将滚动列表所需的行,但滚动条不会反映其位置。最简单的解决方案是重新启用和禁用DGV。
dataGridView1.Enabled = true;
dataGridView1.FirstDisplayedScrollingRowIndex = index;
dataGridView1.Enabled = false;
//这工作,它是区分大小写的,并认为搜索
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;
}
只需设定CurrentCell属性,则DGV将滚动,以使其可见的第一次出现。 –