Very odd Sort problem when modifying DGV bound data

S

sklett

(I posted this in a databinding NG, but it's a VERY low traffic NG so I
thought I would post here as well. I hope no one minds too much, if you do
I'm sorry)

I have a DGV that is bound to a DataTable that is loaded during application
startup. I can sort with all the columns and everything works fine. I have
a method that can be invoked from a context menu that will loop through the
rows of the bound table and modify a specific cell in each row. After
modifying the rows sorting doesn't work for that column anymore.

To give a more familiar example:
Load the Products table from the Northwind DB
Bind to a DGV via a BindingSource object
Test teh sorting by clicking on the DGV columns
add a button and in the click handler, loop through the DataRow collection
and set the ProductID column to the value using an int that increments in
the loop.
Try and sort the ProductID column.

You will get incorrect results, it will sort 20 or so rows, then jump to
another number, then jump again.. it's broken.

I've tried everything I can think of:
calling EndEdit on each row
reseting bindings
etc

Nothing seems to solve it. I'm not one to cry "bug" but this sure seems
like one to me.

Anyone ever experience this? Anyone happen to have a demo application setup
with Northwind they could quickly check this? I've spent over a week now
trying to find the problem and I can't. :(

Any help or input greatly appreciated!

Thanks for reading,
Steve
 
G

Guest

Have you tried re-binding the DGV after you have revised the datasource?
Also, Datatable has a DefaultView property which sports a Sort property that
you can set, and then bind to the View instead of the table.
Peter
 
S

sklett

Thank you for the reply Peter,

Yes, my mistake, I am binding to the DataTable.DefaultView and also
specifying the Sort string via the DataTable.DefaultView.Sort property.

I'm checked the the DefaultView is sorting correctly and it is, if I print
out the values to the console everything is correct, but once the DGV
displays the items it makes a mess of it all.
very strange.
 
R

RobinS

If you are using .Net 2.0, you might try putting a BindingSource between
your data source and your DGV, and using that to sort.

BindingSource myBindingSource = new BindingSource();
myBindingSource.DataSource = myDataTable;
myDataGridView.DataSource = myBindingSource;

Any changes you make to the underlying data source are immediately
displayed to the bound control via the BindingSource. It's kind like
lubrication in a bunch of gears. And you can do sorts and filters and all
kinds of things with the BindingSource.

Robin S.
 
S

sklett

Hi Robin,

Thank you for the reply. I have tried a BindingSource, it didn't make a
difference, same exact behavior :(

Have a good weekend,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top