【问题标题】:Selection changed of datagrid does not changes the current row index数据网格的选择更改不会更改当前行索引
【发布时间】:2017-08-30 19:18:00
【问题描述】:

在我的数据网格中,我正在根据一些值更改行选择。

 dgvGetData.Rows[rowIndex].Selected = true;

但在 Datagrids 选择更改事件中,当前行索引未更改为 rowIndex。在我设置 dgvGetData.Rows[rowIndex].Selected = true; 之前它仍然相同

【问题讨论】:

  • 多选是因为如果我测试它工作正常

标签: c# winforms datagrid


【解决方案1】:

通过使用以下代码,您可以找到一个值,该值将是选定的行:

     int rowIndexResource = -1;
    foreach (DataGridViewRow row in this.resourceDataGridView.Rows)
    {
       if(row.Cells["resourceConfigId"].Value.ToString().Equals(resourceInfo.ResourceConfigId.ToString()))
       {
            rowIndexResource = row.Index;
           break;
        }
    }

this.resourceDataGridView.Rows[rowIndexResource].Selected = true;
this.resourceDataGridView.FirstDisplayedScrollingRowIndex = rowIndexResource;
this.resourceDataGridView.Focus();

“resourceConfigId”是 DataGridView 中列的名称,resourceInfo.ResourceConfigId.ToString() 是您在网格中搜索的特定字符串值。这不是对您问题的直接答案,但您可以考虑这是否仍然有用。

【讨论】:

    【解决方案2】:

    我找到了答案 我将代码设置如下

    rowIndex = row.Index;
                            dgvGetData.ClearSelection();
    
    
                            dgvGetData.CurrentCell = dgvGetData.Rows[rowIndex].Cells[2];
                            dgvGetData.CurrentRow.Selected = false;
                            dgvGetData.Rows[rowIndex].Selected = true;
    

    当我添加行时

    gvGetData.CurrentCell = dgvGetData.Rows[rowIndex].Cells[2];
                                dgvGetData.CurrentRow.Selected = false;
    

    工作并返回选定的行索引作为当前索引..

    【讨论】:

      【解决方案3】:

      您的 DataGridView 对象 FullRowSelect 的属性 SelectionMode 是吗?

      如果您想以编程方式设置属性。 试试这个

      dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      

      【讨论】:

      • 是的,我尝试通过设置 dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      【解决方案4】:

      我的源代码。
      你错过了单元格[ColumnIndex]。

      此代码添加。

      DataGridView.Rows[ RowIndex ].Cells[ ColumnIndex ].selected = true; 
      

      【讨论】:

        【解决方案5】:

        这通常可以正常工作,我能想到的唯一问题是进行多选并取回上一个选定的行(仍会被选中)。如果这是问题所在,那么修复将很容易:

        myDataGridView.ClearSelection();
        myDataGridView.Rows[rowIndex].Selected = true;
        

        【讨论】:

        • 多选属性设置为 false 并且我已经添加了代码 myDataGridView.ClearSelection();在更改选择之前
        • @SruthiSuresh 我在一个新表单上对其进行了测试,它的工作原理非常好,不知道从这里去哪里
        猜你喜欢
        • 2012-08-16
        • 2016-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        相关资源
        最近更新 更多