Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使DataGridView显示所选行?_C#_Winforms_Datagridview_Scroll_Selected - Fatal编程技术网

C# 如何使DataGridView显示所选行?

C# 如何使DataGridView显示所选行?,c#,winforms,datagridview,scroll,selected,C#,Winforms,Datagridview,Scroll,Selected,我需要强制DataGridView显示所选的行 简而言之,我有一个textbox,它根据textbox中键入的内容更改DGV选择。发生这种情况时,选择将更改为匹配的行 不幸的是,如果所选的行不在视图中,我必须手动向下滚动才能找到所选内容。是否有人知道如何强制DGV显示选定的行 谢谢 您可以设置: dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index; 以下是此属性的详细信息。只需将

我需要强制
DataGridView
显示所选的

简而言之,我有一个
textbox
,它根据
textbox
中键入的内容更改
DGV
选择。发生这种情况时,选择将更改为匹配的

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

谢谢

您可以设置:

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

以下是此属性的详细信息。

只需将该行放在选择行之后:

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

这一行滚动到所选行,但不将其置于顶部

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

visibleColumnIndex-所选单元格必须可见

执行以下操作:


dataGridView1.CurrentCell=dataGridView1.Rows[index]。单元格[0]

仅当第一列可见时才有效。如果它是隐藏的,您将得到一个异常。这样更安全:

var column=dataGridView1.CurrentCell!=无效的
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; 
   }
}
}

还应考虑此代码(使用主管技术人员建议的方式):

private static void可修改行(DataGridView视图,int rowthow)
{
if(rowthow>=0&&rowthow=firstVisible+countVisible)
{
view.FirstDisplayedScrollingRowIndex=RowtHostShow-countVisible+1;
}
}
}

请注意,当DataGridView未启用时,设置FirstDisplayedScrollingRowIndex会将列表滚动到所需行,但滚动条不会反映其位置。最简单的解决方案是重新启用和禁用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将滚动以使其可见。差一分钟就错过了!非常感谢。我一直在努力寻找我在使用CurrentCell时的逻辑错误,因为它不能普遍工作。但我可以将我一直使用的行号插入到这个中,它就像一个符咒!简单而完美(y)绝对比DataGridView更方便用户。FirstDisplayedScrollingRowIndex
,谢谢!与FirstDisplayedScrollingRowInde不同,这也会将行箭头移动到正确的行,选择行,并取消选择任何其他行。
dataGridView1.CurrentCell=dataGridView1.SelectedRows[0]。单元格[0]
但是,如果第一列被隐藏,这将引发异常。这是一个非常实用的答案……值得更多的投票。我同意,所以我把它投了更高的票!它比其他任何解决方案都有效。效果很好-我将rowthow设置为opoptional,如果不是由调用者设置,则将其设置为最后一行。现在默认情况下它会滚动到底部。可以再加上一个签名,给它起个更好的名字。谢谢。这比其他答案好得多。其他用户提示:我对其进行了一些修改,以传入firstVisible,因为在我需要调用EnsureRevisibleRow之前,我的列表也在刷新(在刷新DataGridView内容之后,FirstDiaplayedScrollingRowIndex总是重置为零,所以我必须在刷新之前保存它)。小错误:条件应该是:
否则如果(rowToShow>=firstVisible+countVisible-1)
(我刚刚编写了相同的解决方案,因为我错过了这个回复!)这就是我使用的解决方案。
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;
    }