Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Overview:
To be able to pull out data from SQL table, show it in a datagridview, re-order and show the new ordered list in a datagridview after a user as renumbered values in the positioning field.

Attempts:
I've had various attempts at doing this. First was by manipulating the data in the DataGridView object but I hit issues with reentrant errors when trying to use the sort method in the EndCellEdit event. Also events were firing more than once which is a overload that isn't best practice.

This attempt below was to full the data out and show it in the grid. This works up to that part but once I try and minipulate the data in in-memory objects when I put it back to the data source property the grid goes blank as though it doesn't understand the object anymore??

I'm more of a foxpro programmer and normally use CURSORs for this sort of local maniplication of data.

Questions:
1) Why when I loop through a LINQ formed list and change a property the change doesn't remain in place? The only way I've found to keep the changes is to use the .ToList() method?

2) Why doesn't the DataGridView nolonger understand the manipulated collection?

Thanks in advance for any help or advice on this problem.
// str is declared at form level and is initialised with
//
//    from s in evokeDb.vwTblStructs
//    where s.tableisn == tblisn
//    orderby s.order
//    select s;

var tmp1 =
    from n in str
    select n;

IList<vwTblStruct> tmp3 = tmp1.ToList<vwTblStruct>();
foreach(var ord in tmp3)
{
    if (ord.ord == v)
    {
        //this change reverts to orginal data if I don't use .ToList()
        ord.ord = i;
        break;
    }
}

var tmp2 =
    from s in tmp3
    orderby s.ord
    select s;

short order = 1;
foreach (var s in tmp2)
{
    s.order = order;
    order++;
}

str =
    from s in tmp2.AsQueryable<vwTblStruct>()
    orderby s.order
    select s;

grdStruc.DataSource = str;
Posted

1 solution

To answer my own questions.

1) The LINQ statement brought my original collection out as a Queryable type collection. If I converted into a List collection type before binding it to the DataGridView right at the beginning I didn't have any problems when I manipulated the data into different collections as they were all of the List collection type.

2) LINQ statements are defaulted to deferred that means the query is only executed when you start to loop through it. This way there is little overhead as the objects aren't cached. The .ToList() and .ToArray() methods make for an immediate execution that caches the data as a collection of objects.

See working code below:

This is the property defined at the form level:
private List<vwTblStruct> str;


This is the initial data extraction from the SQL server:
C#
str =
    (from s in evokeDb.vwTblStructs
    where s.tableisn == tblisn
    orderby s.order
    select s).ToList();

grdStruc.DataSource = str;


This is the data manipulation code before it databinding it back to the DataGridView:
C#
grdStruc.DataSource = null;

var tmp1 = str.ToList();
foreach(var ord in tmp1)
{
    if (ord.ord == v)
    {
        ord.ord = i;
        break;
    }
}

var tmp =
    (from s in tmp1
    orderby s.ord
    select s).ToList();

short order = 1;
foreach (var s in tmp)
{
    s.order = order;
    s.ord = (decimal)(order + 0.5);
    order++;
}

str = tmp;
grdStruc.DataSource = str;

tmp = null;
tmp1 = null;


I'm open to any suggestions and/or better ways to do accomplish the same result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900