【问题标题】:Select the first row among the visible rows in grid after filter operation筛选操作后选择网格中可见行中的第一行
【发布时间】:2013-02-18 07:31:54
【问题描述】:

我想在过滤操作后选择网格中可见行中的第一行

我尝试按照以下帖子中的代码进行操作,但它不满足当前单元格的条件...

DataGridView: How to select first cell in current row when MultiSelect is true

我尝试了以下代码,但它不起作用

1:

if (grdGLSearch.Rows.GetRowCount(DataGridViewElementStates.Visible) > 0)
   grdGLSearch.Rows[0].Cells[0].Selected  = true;

或 2:

if (grdGLSearch.Rows.GetRowCount(DataGridViewElementStates.Visible) > 0)
   foreach (DataGridViewCell cell in grdGLSearch.Rows[0].Cells)
   {
      cell.Selected = true;
      return;
   }

【问题讨论】:

  • 您说“我想选择网格中的第一(可见)行”然后您尝试选择一个单元格。请给我正确的场景
  • 我相应地编辑了我的问题....

标签: c# winforms datagridview


【解决方案1】:

试试这个:

foreach (DataGridViewRow item in grdGLSearch.Rows)
            {
                if (item.Visible)
                {
                    item.Selected = true;
                    break;
                }
            }

【讨论】:

    【解决方案2】:

    试试这个

    if (grdGLSearch.Rows.GetRowCount(DataGridViewElementStates.Visible) > 0)

      grdGLSearch.Select(0);
    

    【讨论】:

      【解决方案3】:

      您可以使用属性 SelectionMode 来选择 FullRowSelect

      使用此代码

      grdGLSearch.Rows[0].Selected = true;
      

      【讨论】:

      • 这在行 [0] 的 .Visible 属性设置为 false 的情况下无法正常工作。也许您需要重新阅读该问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 2012-11-06
      • 2017-05-23
      相关资源
      最近更新 更多