DataGridView row indices no longer match datasource indices after sort

Posts   
 
    
BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 14-Apr-2008 18:45:02   

LLBLGen Pro Version: 2.5 Final, December 5, 2007 Runtime Library Version: 2.5.7.1214 Code Generation: C#, .NET 2.0, SelfServicing Database: SQL Server 2005

In a WinForms application I have a DataGridView that is bound to an EntityCollection via a BindingSource. The DataGridView is auto-generating its columns based on the EntityCollection.

The problem I'm having is that after the user sorts the grid by clicking on column headers, the row indices in the grid no longer match the row indices in the underlying EntityCollection datasource. This presents a problem when I want to delete a row from the collection based on the current row index in the grid.

Am I missing some setting that keeps the EntityCollection in sync with the grid sort? Or is there some nice way to keep track of which row in the grid matches the row in the collection?

Thanks in advance for your advice. Bret

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 14-Apr-2008 19:39:22   

Could you reproduce your situation in a test solution preferably using Northwind so we can help you better?

BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 14-Apr-2008 21:22:06   

Before I go through the work of putting together a test jig, let me ask a couple general questions.

  1. If I sort a DataGridView by one of its columns, is it true that the EntityCollection that it is bound to does not get sorted to match the new sort order of the rows in the DataGridView?

  2. If #1 is true, is it possible (or even desireable) to change this behavior so that the order of the rows in the EntityCollection always matches the order of the rows in the DataGridView?

  3. If the answer to #2 is that it is not possible (or maybe not desireable) to sort the EntityCollection along with the DataGridView rows, then what is the best way to determine which row in my EntityCollection matches the selected row in the DataGridView, after the DataGridView has possibly been sorted?

I have figured out one possible solution for #3: using the EntityCollection.IndexOf(IEntity) method to search for the current entity by casting my BindingSource.Current object to (EntityBase). For example:

int rowIdx = myEntityCollection.IndexOf((EntityBase)myBindingSource.Current);

But I don't know how efficient that is or if there is a better way to get the row index in my underlying EntityCollection.

Thanks, Bret

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Apr-2008 12:04:23   

The idea is that the DataGridView is not bound to the EntityCollection, but rather to an EntityView (EntityCollection.DefaultView).

So you may use the DefaultView to access the correct entity according to the sorted selected row in the grid.

The question is: do you want to delete the entity from the database or just remove it from the entityCollection. Please check the following thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9721

BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 22-Apr-2008 17:14:50   

Thanks--understanding that the DataGridView is actually bound to an EntityView via the BindingSource rather than directly to the EntityCollection solved my problem.