2011-12-08 69 views
48

我需要强制DataGridView显示选定的row如何使DataGridView显示选定的行?

总之,我有一个textbox,根据输入到textbox的内容更改DGV选择。发生这种情况时,选择将更改为匹配row

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

谢谢!

+7

只需设定CurrentCell属性,则DGV将滚动,以使其可见的第一次出现。 –

回答

86

您可以设置:

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

下面是这个属性的MSDN documentation

+0

谢谢!我一直在努力寻找我的逻辑错误,因为我使用的CurrentCell并不普遍。但是我可以将我一直使用的行号插入到这里,它的功能就像一个魅力! – clweeks

+0

简单和完美(y) –

10

只要把该行的选择行后:

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

错过了一分钟! –

32

这一个滚动所选行没有把它放在上面。

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

绝对比'DataGridView.FirstDisplayedScrollingRowIndex'更加用户友好,谢谢! – Nolonar

+4

与FirstDisplayedScrollingRowInde不同,这也会将行箭头移动到正确的行,选择行并取消选择任何其他行。 – Polyfun

1
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 - 所选单元格必须是可见

0

做这样的事情:

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

如果第一列是可见的才有效。如果它被隐藏,你会得到一个异常。这是更安全:

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

这将重置选择不滚动,如果目标行已经在屏幕上。它还保留当前列选择,这在您允许内联编辑的情况下很重要。

0

我做了下一个搜索功能,它适用于在显示中滚动选择。

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

这是我使用的一个。 – Mac

14

也考虑这个代码(使用从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; 
     } 
    } 
} 
+3

非常实用的答案......值得更多的选票。 – ulatekh

+1

我同意,所以我有upvoted!它比任何其他解决方案都更好。 – JonP

+2

工程很好 - 我做了rowToShow opptional,并将其设置为最后一行,如果未由调用者设置。现在它默认滚动到底部。可以添加另一个签名以给它一个更好的名称。 – rheitzman

0

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

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

//这工作,它是区分大小写的,并认为搜索

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; 
    } 
相关问题