【问题标题】:Set the vertical scroll bar to a particular position when autoscroll is true当自动滚动为真时,将垂直滚动条设置到特定位置
【发布时间】:2026-01-26 13:50:02
【问题描述】:

我有一个数据网格,其中有一行蓝色。我想放置垂直滚动条的位置,使蓝色行可见。我的自动滚动属性设置为 true。因此,每当我尝试为其设置一些值时,垂直滚动条就会消失。

【问题讨论】:

  • 您描述了您想要实现的什么(好)以及您如何尝试的一些细节(不错),但是:您的具体问题? (我喜欢帖子中的说明,所以我不必猜测问题描述和标题的组合。)您的行数是否超过了窗口可以显示的数量?
  • 是的...我的行数超过了窗口可以显示的...

标签: c# winforms


【解决方案1】:

您可以使用此属性设置垂直滚动条的位置。

// Search for the row index you want to display using LINQ
var someRowIndex = dgNew.Rows.Cast<DataGridViewRow>()
    .FirstOrDefault(a => a.Cells["SomeColumnName"].Value != null &&
                         a.Cells["SomeColumnName"].Value.ToString() == "Value of some column in the blue row")?.Index;

if (someRowIndex != null)
{
    dg.FirstDisplayedCell = dg.Rows[someRowIndex].Cells[0];
}

【讨论】:

  • 行索引应该是多少?
  • 您只需使用蓝色突出显示的行的索引。我已经编辑了我的答案,以展示如何使用一些 LINQ 来做到这一点。