|
-
Dec 10th, 2009, 08:58 AM
#1
Thread Starter
Fanatic Member
[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?
-
Dec 10th, 2009, 11:41 AM
#2
Re: Sorting a dgv by 2 columns
you'd sort the linq query before you set your dgv datasource.
can you post your code?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 10th, 2009, 12:30 PM
#3
Thread Starter
Fanatic Member
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
-
Dec 10th, 2009, 12:43 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 10th, 2009, 12:50 PM
#5
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 -
-
Dec 10th, 2009, 01:30 PM
#6
Thread Starter
Fanatic Member
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.
-
Dec 10th, 2009, 02:22 PM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 10th, 2009, 04:12 PM
#8
Thread Starter
Fanatic Member
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
-
Dec 10th, 2009, 05:41 PM
#9
Thread Starter
Fanatic Member
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
-
Dec 11th, 2009, 12:49 AM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 11th, 2009, 05:40 AM
#11
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|