Dataview will not sort

  • Thread starter Raymond Lewallen
  • Start date
R

Raymond Lewallen

I have a dataview in which the sort property will not sort the dataview.
Here's is a simple scenario similar to what I am doing:

Class Foo

Private Function Retrieve() As DataView
' Returns a DataView with 2 columns and 3 rows
Dim ADOHelper As New DAL.ADOHelper
Return ADOHelper.GetMyDataview()
End

Public Sub ShowDataView()
Dim dv As DataView = Retrieve()
' dv's table has 2 columns, integer called "A" and string called "B"
' dv has 3 rows as follows:
' 2 "DEF"
' 3 "GHI"
' 4 "JKL"

Dim drow As DataRow
drow = dv.Table.NewRow()
drow("A") = 1
drow("B") = "ABC"
dv.Table.Rows.Add(drow)

' HERE IS THE PROBLEM. SORT DOES NOT SORT.
dv.Sort = "A"

For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.ConCat(eachRow.Item("A").ToString(),
" - ", eachRow.Item("B").ToString()))
Next

End Class

When calling Foo.ShowDataView(), the output will be the following:

2 - DEF
3 - GHI
4 - JKL
1 - ABC

Any ideas on why the sort is not working? Is there something else I am
missing? I've applied sorts to all kinds of dataviews before and never had
this happen, and I've spent 6 hours trying to figure out why it will not
work. I've even tried sorting on column "B", and tried sorting DESC on each
column too. Nothing seems to work.

Any help is greatly appreciated.
 
G

Guest

The View sort and the "sort" on the table are two different things. Try
binding the DataView to some control in your application and looking at the
order rather than looping through the original rows.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
J

Jim Hughes

It appears that it is not sorted because you are using For Each on the
DataTable instead of looping through the sorted rows in the DataView by
index

Try this instead:
For i As Integer = 0 To dv.Count - 1

Dim eachrow As DataRowView = dv.Item(i)

Console.WriteLine(String.Concat(eachrow.Item("A").ToString(), " - ",
eachrow.Item("B").ToString()))

Next
 
J

Jim Hughes

You could also use:

Dim iterator As IEnumerator = dv.GetEnumerator
While iterator.MoveNext

Dim drv As System.Data.DataRowView = CType(iterator.Current,
System.Data.DataRowView)

Console.WriteLine(String.Concat(drv.Item("A").ToString(), " - ",
drv.Item("B").ToString()))

End While
 
C

Cor Ligthert

Raymond,

Does this show it you?

\\\Using the original table from the dataview
For Each eachRow As DataRow In dv.Table.Rows
Console.WriteLine(String.Concat(eachRow.Item("A").ToString(),
"-", eachRow.Item("B").ToString()))
Next
///
\\\Using the dataview
For Each eachRow As DataRowView In dv
Console.WriteLine(String.Concat(eachRow.Item("A").ToString(),
"-", eachRow.Item("B").ToString()))
Next
///

Cor
 
R

Raymond Lewallen

The view sort and the sort on the base table are 2 different things, that's
all binding and looking at it will confirm, and you already told me that's
the case, and I trust you so no need to see it for myself. I need to know
the best way to take the sorted dataview and loop through it in sorted form
so my console.write will output sorted information. How can I get the base
table sorted? Probably can't. Will I have to do DataView.CopyTo to move
the contents into an array, sort the array and then pull out the
information? How are other people solving these issues?
 
R

Raymond Lewallen

That's what I was looking for. Thank you. I'm a library writer and don't
spend much time on these types that are generally used for controls and GUI.
 
M

Miha Markic [MVP C#]

Hi Raymond,

Raymond Lewallen said:
The view sort and the sort on the base table are 2 different things,
that's
all binding and looking at it will confirm, and you already told me that's
the case, and I trust you so no need to see it for myself. I need to know
the best way to take the sorted dataview and loop through it in sorted
form
so my console.write will output sorted information.

foreach (DataRowView rowView in view)
Console.WriteLine(rowView["Column"]);

How can I get the base
table sorted? Probably can't. Will I have to do DataView.CopyTo to move
the contents into an array, sort the array and then pull out the
information? How are other people solving these issues?

Why do you need to sort table?
Why don't you just use DataView?
 
R

Raymond Lewallen

Cor,

Yes, your solution does work. Thank you for posting it. Sorry to bother
everyone with what seems to be such a remedial question, but I spend most of
my time in the database and writing supporting libraries, so using these
types that are more common to the GUI and used to bind to controls is not
something I spend much time doing, and DataRowView doesn't come up in very
easily to find places when looking through the help on dataviews.

Thanks again to Cowboy, Jim and Cor.
 
R

Raymond Lewallen

Why do you need to sort table?

I don't now that I know there is a rows collection of sorts for the
dataview, i.e. the DataRowView. Thats not something that was easy to find
associated with a dataview when looking up dataview and sorting in the help
files.
Why don't you just use DataView?

I am now that I have your solution along with Jim's and Cor's.

Thank you for posting a solution,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top