Results 1 to 11 of 11

Thread: [RESOLVED] Sorting a dgv by 2 columns

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved [RESOLVED] Sorting a dgv by 2 columns

    I'm sure this has been asked here before but i couldn't find it, anyway my issue is i have filled a dgv using a LINQ query and i need to sort my results by 2 columns, i.e. type and cost so that it lists items by each type in creasing cost

    Accessories 15
    Accessories 20
    Accessories 55
    Clothing 22
    Clothing 34
    Clothing 72


    all columns in the dgv need to obviously sort according to the above 2 criteria.

    My research seems to suggest that i have to do this programmatically so can someone point me in the right direction?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sorting a dgv by 2 columns

    you'd sort the linq query before you set your dgv datasource.
    can you post your code?

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Sorting a dgv by 2 columns

    sure i can provide code
    Code:
         If Not IO.File.Exists("list.xml") Then
             Dim URL As String = "http://www.somesite.com/items.xml"
             Dim webClient As New System.Net.WebClient
             webClient.DownloadFile(URL, "list.xml")
             Application.DoEvents()
          End If
    
          Dim mItemDocument As XDocument
          mItemDocument = XDocument.Load("list.xml")
    
          Dim items = _
             ( _
                From items In mItemDocument...<item> _
                Select New With _
                   { _
                      .Type = items.<type>.Value, _
                      .Quantity = items.<quantity>.Value _
                   } _
          ).ToList
    
          If Not items Is Nothing Then
             DataGridView1.DataSource = items
          Else
             MsgBox("Error")
          End If

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sorting a dgv by 2 columns

    try this:

    vb Code:
    1. If Not IO.File.Exists("list.xml") Then
    2.     Dim URL As String = "http://www.somesite.com/items.xml"
    3.     Dim webClient As New System.Net.WebClient
    4.     webClient.DownloadFile(URL, "list.xml")
    5.     Application.DoEvents()
    6. End If
    7.  
    8. Dim mItemDocument As XDocument
    9. mItemDocument = XDocument.Load("list.xml")
    10.  
    11. Dim items = _
    12.            ( _
    13.               From items In mItemDocument...<item> _
    14.               Select New With _
    15.                  { _
    16.                     .Type = items.<type>.Value, _
    17.                     .Quantity = items.<quantity>.Value _
    18.                  } Order By .type Ascending, .quantity Ascending _
    19.         ).ToList
    20.  
    21. If Not items Is Nothing Then
    22.     DataGridView1.DataSource = items
    23. Else
    24.     MsgBox("Error")
    25. End If

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Sorting a dgv by 2 columns

    Why don't you use a bindingsource and set its Sort property?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Sorting a dgv by 2 columns

    .Paul i tried what you suggested but it came up with an error saying leading '.' or '!' can only appear inside a 'With' statement. i will however lookinto sorting with LINQ and see what i can find

    stanav not sure how to go about doing that.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sorting a dgv by 2 columns

    ok i've tested it now. this should work:

    vb Code:
    1. If Not IO.File.Exists("list.xml") Then
    2.     Dim URL As String = "http://www.somesite.com/items.xml"
    3.     Dim webClient As New System.Net.WebClient
    4.     webClient.DownloadFile(URL, "list.xml")
    5.     Application.DoEvents()
    6. End If
    7.  
    8. Dim mItemDocument As XDocument
    9. mItemDocument = XDocument.Load("list.xml")
    10.  
    11. Dim items = _
    12.            ( _
    13.               From items In mItemDocument...<item> _
    14.               Order By cstr(items...<type>.First()) Ascending, cint(items...<quantity>.First()) Ascending _
    15.               Select New With _
    16.                  { _
    17.                     .Type = items.<type>.Value, _
    18.                     .Quantity = items.<quantity>.Value _
    19.                  } _
    20.         ).ToList
    21.  
    22. If Not items Is Nothing Then
    23.     DataGridView1.DataSource = items
    24. Else
    25.     MsgBox("Error")
    26. End If

  8. #8

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Sorting a dgv by 2 columns

    hi .paul, tried it again seems to still not be right, changed the new line to

    Code:
    Order By cstr(items...<type>.Value) Ascending, cint(items...<quantity>.Value) Ascending _
    and all works fine now

  9. #9

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Sorting a dgv by 2 columns

    well now that i have ordered my list i have spotted a few items with a quantity of 0 i don't wish to include these in my query, anyway i could do that prior to displaying the dgv? This linq is hard to take in lol

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sorting a dgv by 2 columns

    vb Code:
    1. If Not IO.File.Exists("list.xml") Then
    2.     Dim URL As String = "http://www.somesite.com/items.xml"
    3.     Dim webClient As New System.Net.WebClient
    4.     webClient.DownloadFile(URL, "list.xml")
    5.     Application.DoEvents()
    6. End If
    7.  
    8. Dim mItemDocument As XDocument
    9. mItemDocument = XDocument.Load("list.xml")
    10.  
    11. Dim items = _
    12.            ( _
    13.               From items In mItemDocument...<item> _
    14.               Order By cstr(items...<type>.Value) Ascending, cint(items...<quantity>.Value) Ascending _
    15.               where cint(items...<quantity>.Value) > 0 _
    16.               Select New With _
    17.                  { _
    18.                     .Type = items.<type>.Value, _
    19.                     .Quantity = items.<quantity>.Value _
    20.                  } _
    21.         ).ToList
    22.  
    23. If Not items Is Nothing Then
    24.     DataGridView1.DataSource = items
    25. Else
    26.     MsgBox("Error")
    27. End If

  11. #11

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved Re: Sorting a dgv by 2 columns

    Nice I have to admit again that i am a big fan of linq, not using linq this program would have taken a few pages of code whereas with linq half a page, massive saver on time programming and it runs superfast too (not an I'm not worthy icon lol) thanks for the help .paul

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width