Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
165
Problem setting ActiveRow on a grid
posted

My grid is bound to a data table ...  and one of its rows is selected (making an ActiveRow property of the grid).  At some point I would like to refresh the grid by assingning a new insance of the table as its data source ... keeping the same row active.

The problem I have is, when I set new table as the grid's data source, and assign an active row, the grid control sets its own row as an active row (shortly after that).  I traced the problem and found out that the grid in its paint method calls EnsureTempRowIsSelected (or similar) method which assigns "temp row" as the active row ... that comes after I set my row as the active row and as a result, a wrong row ends up being an active row.

 

When assigning a new table as grid's data souce I do the following (pseudocode):

row = grid's active row

rowIndex = row.Index

grid.DataSource = <new insance of table>

grid.ActiveRow = grid.Rows(rowIndex)

 

If somebody has had a similar problem, or knows how I could resolve this problem, please let me know.

 

Thank you very much!

 

  • 469350
    Offline posted

    Hi,

    Just a point of terminology... don't confuse "active" and "selected". These are two different things and they are not interchangable. It sounds like you are talking about the ActiveRow here, not the selected row. :)

    Anyway, the grid doesn't actually set an ActiveRow automatically. What it does do is synchronize it's active row with the current position of the BindingManager. And it does this lazily when the grid paints. So there are a couple of ways you could get around this. 

    One way would be to set the Current property of the BindingManager, rather than setting the ActiveRow on the grid. 

    Another would be to use a BeginInvoke to set the grid's ActiveRow, rather than doing it directly after setting the DataSource. 

    Still another solution might be to set the grid's SyncWithCurrencyManager property to false so they the grid doesn't do any synchronization. 

    And yet another possible solution might be to call Refresh on the grid after setting the DataSource (to force a paint) before setting the ActiveRow.

    If none of the those work, let me know and I will see what else I can come up with. :)