2017-04-05 36 views
0

在我的数据网格中,正在根据某些值更改行选择。选择已更改的数据网格不会更改当前行索引

dgvGetData.Rows[rowIndex].Selected = true; 

但在DataGrid中选择改变事件的当前行的索引不改变的rowIndex.it仍然是我设置之前相同dgvGetData.Rows[rowIndex].Selected = true;

+0

是多选,因为如果我测试它工作正常 – EpicKip

回答

1

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

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; 

奏效并返回选定行的索引作为当前index ..

0

这通常工作得很好,只有我能想到的是问题有多重选择并取回先前选定的行(即将被选中)。如果这将是问题,修复将很容易:

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

multiselect属性设置为false,我也alresdy添加了代码myDataGridView.ClearSelection();改变选择 –

+0

@SruthiSuresh我在一个新的窗体上测试它,它的工作原理如此idk从这里去 – EpicKip

0

我的源代码。
you miss cells [ColumnIndex]。

此代码添加。

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

是您的DataGridView对象FullRowSelect的属性SelectionMode

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

dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
+0

yess我试图通过设置dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; –

0

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

 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()是你在后搜索特定的字符串值格。这不是对你的问题的直接回答,但你可以考虑这是否仍然有用。

相关问题