Home Forums WinForms controls Xceed Grid for WinForms Sort & Primary Key.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14509 |

    Hello everybody,
    I’m trying Xceed.Grid and I’ve found a very strange behaviour.
    It’s very simple to reproduce it. I load a datatable using a dataadapater/sqlcommand
    with a simple sql statement (SELECT * FROM Table). Then I assing
    this datatable to a GridControl.DataSource.
    Then if I click twice on a cloumnmanager cell, GridControl object order its content
    depending first on column I clicked on and then using Primary Key defined in the
    SqlSchema.
    (I have a datatable with some columns ID, Name, and Surname where ID is defined as
    PRIMARY KEY. I want the datatable to be ordered by Surname DESC, while GridControl order it by Surname DESC, ID DESC)
    This seem to be default behaviour of GridControl. Is it true?
    I can’t understand why it has been implemented in this way!!
    There is a way to avoid it?

    Thanks in advance
    Skysurfer72

    Imported from legacy forums. Posted by Gian Luigi (had 2961 views)

    User (Old forums)
    Member
    Post count: 23064

    It is an optimization question. When the data is first loaded in the Grid with no sort, the order of the rows will be that of the dataset, which we could call the “natural” sort. Most often, the dataset is already sorted on the primary key, which is usually an ID. If you sort on a column other then the ID, the first sort will be ASC, and the duplicate rows will still be sorted on the “natural sort”. If you sort again, that is, having a DESC sort, ALL the rows will be inverted, and thus, on the duplicate rows, the “natural” sort will also be inverted, that is, DESC. This is done because the grid doesn’t have to sort again when going from ASC to DESC, it only has to invert the whole grid. One can also argue that it is the expected behavior, that when going from ASC to DESC, all rows should be inverted.

    In you case, it happens that the natural sort is the same as the primary key, and that is why you see ID DESC.

    If you want to keep ID ASC, a possible workaround would be to keep the ID column sorted ASC by code, with the help of the SortedColumnsChanged event on the GridControl. For example, once the grid as been sorted on the Surname DESC, you add a new sort on the ID, and make it ASC.

    Imported from legacy forums. Posted by André (had 632 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi
    I understand your point of view.
    Let me say that in SQL Query Analizer if I execute a SQL statement
    “SELECT * FROM Table ORDER BY Field DESC” it doesn’t show the
    result ordered also BY ID (where ID is the Table’s PRIMARY KEY) DESC.

    Furthermore I assign the datasource to gridcontrol as shown below
    GridControl.DataSource = myDataview.
    To be able to modify data in datasource (we use a normal edit form
    to modify data stored in a DatarowView item collection) I assign
    myDataview.Sort=GridControl.SortedColumns.ToString and so
    the data in the two collections (GridControl.Rows collection
    and myDataview.item collection) aren’t in the same order.
    This because myDataview doesn’t invert the whole grid,
    but only apply specified sort. So when I select a row in GridControl,
    and press a button to open the form to modify SelectedRows data,
    the form is loaded with wrong row item (due to different order).
    I try your workaround, but I wasn’t able to let it work,
    because when I first click on Column to have it orderd ASC,I do nothing,
    then when I sort again (from ASC to DESC) by code I add ID ASC.
    This is working but is also changing standard behaviour of gridcontrol,
    where there is a cicle ASC—>DESC–>nothing—>ASC—>DESC–>nothing,
    while doing in this way we obtain ASC—>DESC,ASC—>ASC—>DESC,ASC
    with no way to remove sort.
    Above all I have to say that we would like to use it to change a lot of datatable,
    not only one table, and ‘natural’ order isn’t always the primary key of the table,
    but change depending from the datatable, and so is not so simple to found which
    columns determines which is the ‘natural’ order, and if SortedColums collections
    already contains it and so on…

    Can you send me a little example that will help in my testing scenario?

    Thanks in advance
    SkySurfer72

    Imported from legacy forums. Posted by Gian Luigi (had 457 views)

    User (Old forums)
    Member
    Post count: 23064

    Actually, you don’t need to keep the two collections sorted the same way. You can send the SourceObject of the SelectedRows to the edit form to get the right rows, instead of using the myDataView. For example :

    ArrayList mySourceObjects = new ArrayList();

    foreach( Row selectedRow in gridControl1.SelectedRows )
    {
    mySourceObjects.Add( ( ( Xceed.Grid.DataRow )selectedRow ).SourceObject );
    }
    //pass mySourceObjects to the edit form.

    You can look at the following <a href=”http://www.xceedsoft.com/Forums/ShowPost.aspx?PostID=4396″&gt; forum</a> to see an example of using the SourceObject.

    Imported from legacy forums. Posted by André (had 4014 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.