Delete last row in DataGrid

Posts   
 
    
Posts: 6
Joined: 22-Jan-2008
# Posted on: 22-Jan-2008 16:56:18   

Hi,

I have an standard .NET DataGrid (WinForms) bounded to a LLBLGen Collection object somewhat like this:

_customers = new CustomerCollection();
_customers.GetMulti(null);

_customers.SupportsSorting = true;
_customers.Sort((int)CustomerFieldIndex.LastName, ListSortDirection.Ascending);

dataGrid.DataSource = _customers;
dataGrid.TableStyles.Add(new DataGridTableStyle());
dataGrid.TableStyles[0].MappingName = "CustomerEntityCollection";

All is well, but when I want to delete the last row in the grid I get a System.ArgumentOutOfRangeException: "Specified argument was out of the range of valid values. Parameter name: Index was out of range. Must be non-negative and less than the size of the collection." It occurs at "System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[] localGridRows)".

If I do not apply the sorting to the collection, the error doesn't occur.

I am using: - .NET 1.1 / C# - LLBLGen Pro v2.0.0.0 Final - SqlServer / SelfServicing

Does anyone has an explanation, or even better, a solution for this problem?

Thanks in advance...

Bye,

Patrick Vroegh

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Jan-2008 10:26:21   

Please post the code that produced the exception. Also post the LLBLGen Pro runtime library version. (consult: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7720)

Posts: 6
Joined: 22-Jan-2008
# Posted on: 23-Jan-2008 10:38:51   

Hi,

Sorry for the incomplete post wink

The version of the runtime I'm using is 2.0.07.0611.

The following code is part of Form1.cs:

        private AuthorsCollection _authors;

        private void button1_Click(object sender, System.EventArgs e)
        {
            // Haal alle Authors op.
            _authors = new AuthorsCollection();
            _authors.GetMulti(null);
            
            // Voeg een tablestyle toe aan het grid
            // met de juiste mapping naam
            dataGrid1.TableStyles.Add(new DataGridTableStyle());
            dataGrid1.TableStyles[0].MappingName = "AuthorsEntityCollection";
            // Koppel de collectie met Authors aan het grid.
            dataGrid1.DataSource = _authors;
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            // Sorteer op achternaam.
            _authors.SupportsSorting = true;
            _authors.Sort((int)AuthorsFieldIndex.AuLname, System.ComponentModel.ListSortDirection.Ascending);
        }

When you click on Button1 the grid will be populated with data from the Authors table. At this point it is possible to delete the last row (->select whole row->delete key). But when you click Button2 and then try to delete the last row, the application generates an exception. It looks like the databinding is out of sync when an EntityCollection is sorted.

I hope I have specified enough information.

Greets, Patrick Vroegh

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Jan-2008 11:36:12   

I don't have VS 2003 to test it, but I think you should re-bind after sorting the collection.

Posts: 6
Joined: 22-Jan-2008
# Posted on: 23-Jan-2008 13:06:26   

Hi,

I tried that already like this:

        private void button2_Click(object sender, System.EventArgs e)
        {
            // Sorteer op achternaam.
            dataGrid1.DataSource = null;    
            _authors.SupportsSorting = true;
            _authors.Sort((int)AuthorsFieldIndex.AuLname, System.ComponentModel.ListSortDirection.Ascending);
            dataGrid1.DataSource = _authors;
        }

But unfortunately that doesn't solve the problem. The exception is still being thrown.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 23-Jan-2008 13:35:37   

Please update to the latest build of the runtime lib for v2.0, it might be a bugfix on jun 21st 2007 fixed this problem.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 6
Joined: 22-Jan-2008
# Posted on: 23-Jan-2008 13:54:51   

Already tried, but that doesn't do the trick. I also tried it on v2.5 of LLBLGen but with no result.

The bug you're referring to (06/21/2007) is a bugfix for .NET 2.0, but I'm using .NET 1.1.

confused

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Jan-2008 15:32:20   

Would you please post a simple repro project? Thanks.

Posts: 6
Joined: 22-Jan-2008
# Posted on: 23-Jan-2008 15:40:06   

Ok -posted-

I'm using the pubs database on my localhost (SQL 2000 server)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 23-Jan-2008 17:57:19   

Looking into it. Very strange though... the collection is bound to the grid using the entityview object, so sorting the collection has no real effect on how the binding works...

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 23-Jan-2008 18:29:46   

You should use the view, and not the collection to sort the elements to bind. This works:

private void button2_Click(object sender, System.EventArgs e)
{
    // Sorteer op achternaam.
    dataGrid1.DataSource = null;    
    IEntityView view = _authors.DefaultView;
    view.Sorter = new SortExpression(AuthorsFields.AuLname | SortOperator.Ascending);
    dataGrid1.DataSource = view;    
}

Clicking other columns also sorts the columns and removing the last row works properly.

The collection is bound through its DefaultView (which is an EntityView instance), as the collection implements IListSource, so the grid will ask IListSource.GetList() on the collection for the real object to bind to. The collection will return the DefaultView instance. You can also create your own view and bind to that.

This is recommended as Sorting the collection itself is deprecated, it's a left over of the IBindingList implementation on the collection.

Now, of course you wonder, why doesn't it work? Hard to tell. The sort routine does raise ListChange(reset ) on the collection after the sort action. This signals the view to reset itself and the grid acknowledges that as it repaints the grid, sorted. However, that it still gets confused... no idea.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 6
Joined: 22-Jan-2008
# Posted on: 23-Jan-2008 18:38:58   

Wow, works like a charm!

Thanks for the excellent support!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 23-Jan-2008 18:41:24   

Patrick_Vroegh wrote:

Wow, works like a charm!

simple_smile I'm glad, coz those views took a while to write wink . You can filter them too btw, and sort on multiple fields, without touching the original collection (so create 2 or more on the same collection is no problem wink )

Thanks for the excellent support!

Geen dank simple_smile

Frans Bouma | Lead developer LLBLGen Pro