none
Problem sorting a bound datagridview

    Question

  • I am struggling with what seems like should have an easy solution. I have datatable bound to a datagridview with 3 columns P_ID, S_ID, Sort. The first 2 fields are a primary key. I want the user to be able to reorder the items in the datagrid using up/down buttons. So it should be a simple swap of the sort number then re-sort. But the problem is once the sort order is set for a datagrid column it stays that way and when I start to swap numbers again the rows change before the number swap is complete and it causes issues. I have tried swapping numbers in the datatable but there is no way to re-sort it without calling the tableadapter.update then .fill, which was causing some random update issues. Anyone have the solution to this?
    Monday, March 27, 2017 2:28 PM

Answers

  • If using a TableAdapter (meaning the IDE generated classes for your data) then there is a BindingSource associated with the TableAdapter and DataSet. This means you can use the BindingSource to sort.

    Sort SomeBindingSource.Sort = "ColumnNameToSort (can also do ColumnNameToSort DESC" for reverse sort

    Unsort SomeBindingSource.Sort = ""


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BillB442 Monday, March 27, 2017 4:11 PM
    Monday, March 27, 2017 3:20 PM
    Moderator

All replies

  • If it is bound then it is bound, but you wrote you use a datatable. 

    Bind then instead of direct to the datatable to the defaultview (is a dataview) and set the sort of that. 

    dt.defaultview.sort = "TheOrder"


    Success
    Cor


    • Edited by Cor LigthertMVP Monday, March 27, 2017 2:45 PM
    • Proposed as answer by leshay Monday, March 27, 2017 3:05 PM
    Monday, March 27, 2017 2:45 PM
  • If using a TableAdapter (meaning the IDE generated classes for your data) then there is a BindingSource associated with the TableAdapter and DataSet. This means you can use the BindingSource to sort.

    Sort SomeBindingSource.Sort = "ColumnNameToSort (can also do ColumnNameToSort DESC" for reverse sort

    Unsort SomeBindingSource.Sort = ""


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BillB442 Monday, March 27, 2017 4:11 PM
    Monday, March 27, 2017 3:20 PM
    Moderator
  • Thanks for the reply. I am using a bindingsource control and I dont see any way to bind it to the defaultview. only the datatable shows in the datamember dropdown. Is programatically the only way to bind to the default view? what is the difference from sorting the default view and sorting the binding control?

    Also, that doesnt really answer my issue with swapping the numbers. Once sort is enabled as soon as I change one number it re-sorts before I can change the other causing the rows to move unexpectedly. Where should I be doing the swap of the numbers? In the datagridview, bindingsource, datatable?

    Monday, March 27, 2017 3:22 PM
  • Thank you Karen. I tried using the bindingsource.sort and that seems to have done the trick. Although I am not sure why I am not running into the same issue with the rows moving like when I sorted by the datagridview column. But, In any case, it seems to be solved. Thanks.
    Monday, March 27, 2017 4:11 PM
  • Thank you Karen. I tried using the bindingsource.sort and that seems to have done the trick. Although I am not sure why I am not running into the same issue with the rows moving like when I sorted by the datagridview column. But, In any case, it seems to be solved. Thanks.

    Bill that is easy. 

    The datasource had some issues for binding direct, therefore there is made an extra class to be an intermidiate between the datatable and the datasource. 

    Please tell next time correct that you use a bindingsource between your datatable and the datasource. Then I had given this reply direct. 


    Success
    Cor

    Monday, March 27, 2017 8:08 PM