Data Grid View...programmatically setting the select row index doesn't set the CurrentRow.Index to the same?
Asked Answered
F

3

6

This code

CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index

stores the current selected row that has been clicked by the user in a data grid view control . After refreshing the datasource for the data grid view, this code

Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True

programmatically re-selects the same row.

Immediately afterwards though

 Me.dgvPreviouslyCut.CurrentRow.Index

is always set to zero, NOT the variable CurrentSelectedRow as you would expect.

Why does programmatically setting the select row index not cause the property CurrentRow.Index to be set to the same?

Fayfayal answered 10/3, 2014 at 15:44 Comment(0)
C
18

CurrentRow is the row containing the currently active cell. When you bind the DataGridView to an external data source, this property is reset to its default value which is the first cell in the first column.

SelectedRow is the row which is currently selected/highlighted. It may be one or more rows depending on MultiSelect property. To select a row you have to set its Selected property to true.

By setting the row as selected you merely keeping it highlighted not making it active.

To retain the current cell you have to store the Current cell's row and column index. To get them use the CurrentCellAddress property. Once your refresh the DataSource set the CurrentCell property using these indexes.

dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);
Charbonneau answered 10/3, 2014 at 16:15 Comment(3)
I've tried setting the current cell, but it seems to only accept a cell object from the current version of the datagridview. And I haven't found a way to specify the currrent cell programatically. There's no row property in a datagridview. Are you sure your code is correct?Fayfayal
@Fayfayal Sorry for the typo in code. I have modified my code. Yes, you have to set the currentcell from the current version of the datagridview. Thats why I am getting the cell using the indexes from the datagridview.Charbonneau
just for completion of you right and useful answer. ColumnIndex must be an index of a visible column, or it will trow an exceptionChimb
E
1

The DataGridView creates a new CurrencyManager when the data source is changed. If this CM contains items, the default position is 0, thus pushing this to the DGV and selects the first row.

To fix this, just set the position of the CM instead:

Me.dgvPreviouslyCut.DataSource = my_new_datasource

Dim cm As CurrencyManager = CType(Me.BindingContext(my_new_datasource), CurrencyManager)

If ((Me.CurrentSelectedRow > -1) AndAlso (Me.CurrentSelectedRow < cm.Count)) Then
    cm.Position = Me.CurrentSelectedRow
End If
Evy answered 10/3, 2014 at 16:25 Comment(3)
My data source is a recordset from SQL Server 2008 database. It's Count property is 1. I'm thinking that's because it has one datamember, which is called 'CurData'. How would I change the position inside of that datamember?Fayfayal
@Fayfayal A recordset is pretty vague, what is the object type?Multivalent
It's actually a dataset. I used the other suggestion as it was a little more straightforward. Thanks for the help though, I know a little more about bindings now!Fayfayal
C
0

A simple trick is to get the current row index:

int RowNumber = YourDataGridView.CurrentRow.Index;

After refreshing the datasource/datagridview, you can re-select the previous row index using:

YourDataGridView.ClearSelection();
YourDataGridView.CurrentCell = YourDataGridView.Rows[RowNumber].Cells[0];

That should re-select the previowsly selected row with the correct index.

Cartwell answered 18/2 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.