Datagridview bound to datatable, sort problem

weblamer

New member
Joined
Sep 27, 2007
Messages
2
Programming Experience
3-5
Hello all, I have a question I hope someone can help me with. I have seen similar questions in the thread, but none that quite cover my problem.

I am writing an application in vb.net. It uses an odbc connector to download an sql query from a mysql database. This data is placed into a data table. I then have the datagridview bound to the datatable, IE:

DataGridView1.DataSource = MyDataTable.DefaultView

I also have text boxes on my form that holds the same information as the datatable. I am using a global integer named DataPos to use as a position market to 'move' through the datatable. I have 'move forward' and 'move last' buttons that add or remove from the global position number, and then a function called 'update' that populates all my text boxes: ie:

Me.Cost1.text = MyDataTable.rows(DataPos)("Cost1")
Me.Shipping.text = MyDataTable.rows(DataPos)("Shipping")


and so on.

Now, when I load the data into the datatable and the dataviewgrid, it all works fine. I can use the back and forward button to move through the data. However, when I sort the datagridview by clicking on a column, the 'index' of the datagridview and the datatable go out of sync. So when the user clicks the next button, what is showing up on the datagridview and what is in the text boxes are two different things.

Does anyone know how I can sort the index (is that what it is called?) of the datatable to match up with that is on the grid?

thanks.
 
Good grief, you like making things hard work eh?

A positional notator already exists. Read up on BindingSource

Bind your text boxes and your datagridview all through the same bindingsource and they will all change in sync. BS can add and remove rows, filter and sort too..
 

Latest posts

Back
Top