[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?
Re: Sorting a dgv by 2 columns
you'd sort the linq query before you set your dgv datasource.
can you post your code?
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
Re: Sorting a dgv by 2 columns
try this:
vb 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 _
} Order By .type Ascending, .quantity Ascending _
).ToList
If Not items Is Nothing Then
DataGridView1.DataSource = items
Else
MsgBox("Error")
End If
Re: Sorting a dgv by 2 columns
Why don't you use a bindingsource and set its Sort property?
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.
Re: Sorting a dgv by 2 columns
ok i've tested it now. this should work:
vb 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> _
Order By cstr(items...<type>.First()) Ascending, cint(items...<quantity>.First()) Ascending _
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
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 :)
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
Re: Sorting a dgv by 2 columns
vb 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> _
Order By cstr(items...<type>.Value) Ascending, cint(items...<quantity>.Value) Ascending _
where cint(items...<quantity>.Value) > 0 _
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
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 :cool:(not an I'm not worthy icon lol) thanks for the help .paul :check: