DataGrid refreshing


Active member
Mar 27, 2008
Programming Experience
Hello there. What I am breaking my teeth on right now is this: I have a simple database application that shows data using a datagridview. This control is bound to a DataView (so I can apply filtering). The Dataview is bound to a DataSet which is then filled from a DataAdapter using Fill method.
The application connects to a remote computer and there can be several clients doing the same - adding, editing or deleting records. To keep track of changes, I need to refresh (or re-read) the data from time to time, so each client keeps track of recent changes. Now I do it this way:

DataAdapter.Fill(DataSet, "MyTable")

If I don´t clear the DataSet´s table contents, the datagrid would show only current and newly added records, but also keep displaying deleted records.

Now to the point: the problem is that each time the datagrid is refreshed, it is basically cleared and filled again, loosing the information of currently selected row. After refreshing, the first row always gets selected. My question is: how not to loose the currently selected row?

Now I use these steps to partially overcome this issue:

- get the value (IDa) in the column called "ID" (distinct value) on currently selected row
- perform the refresh
- loop through the displayed rows: if ID of a row equals IDa then select that row

It does the job, but it also scrolls the selected row out of view and I am afraid that it won´t be much effective when there are many rows in the future.

Is there a simpler way?



VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
First up, there's no point binding to a DataView. The only reason to create a DataView is if you want to sort or filter the same data in more than one way. Every DataTable is already associated with a DataView, which you can access via its DefaultView property. Also, when you bind a DataTable to a control it's actually the contents of that DefaultView that gets displayed. Furthermore, you should generally do your data-binding using a BindingSource. In your case, bind the DataTable to a BindingSource, bind that to your grid and then you set the BindingSource.Filter property.

Now that you're using a BindingSource you can do this:
Dim currentID As Integer = CInt(DirectCast(myBindingSource.Current, DataRowView)("ID"))

'Clear the table and repopulate it.

myBindingSource.Position = myBindingSource.Find("ID", currentID)
That's as good as it gets.

Latest posts

Top Bottom