DataTable.DefaultView.Sort not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a DataTable that I want to sort, the Call_Sort_Order is an Int16.

The order of rows to start with is 1, 2, 3, 5, 4 and is exactly the same after the sort.

Example Code:
dtCalls.DefaultView.Sort = "Call_Sort_Order"

I've even tried applying a filter:
dtCalls.DefaultView.RowFilter = "Call_Sort_Order >= 3"
This doesn't work either.

I have also tried, without effect:
Dim dv As New DataView(dtCalls, "", "Call_Sort_Order", DataViewRowState.OriginalRows)

Does anyone have an ideas what I am doing wrong?

Thanks in advance.
Chris.
 
DataRowView does not sort the rows, but rather DataRowView elements of the
DataView. If you bind a DataGrid or other control to the
DataTable.DefaultView after applying the sort, you should see the sort
results.
If you are familiar with SQL, DataTable is like the table and DataView is
more like a SQL view built on a select statement like
SELECT * from Table Order by SortField. Using the View does not alter the
record order in the original table
 
Alex,

Thanks for the reply.

The ListView I am populating is bound to the DataSource. The person who originally wrote the code intended it that way (not sure why and he can't remember). I have reworked their code and I have added some code to sort the ListView, this is sufficient for our requirements. I don’t have time to recode it to use a DataSource.

Thanks for your help.
Chris.
 
ListView is not a databound control. It has no DataSource property

I suspect what you currently have in your code is something like this:

foreach( DataRow row in table.Rows)
{
ListViewItem item = new ListViewItem(row["field1"].ToString());
item.SubItems.Add ...
}

You want to replace it with

foreach( DataRowView rw in table.DefaultView )
{
DataRow row = rw.Row;
ListViewItem item = new ListViewItem(row["field1"].ToString());
item.SubItems.Add ...
}


--
Alex Feinman
---
Visit http://www.opennetcf.org
Chris Podmore said:
Alex,

Thanks for the reply.

The ListView I am populating is bound to the DataSource. The person who
originally wrote the code intended it that way (not sure why and he can't
remember). I have reworked their code and I have added some code to sort the
ListView, this is sufficient for our requirements. I don't have time to
recode it to use a DataSource.
 
Alex,

To be honest I never checked if the ListView was a data bound control.
Thanks for the sample code, I'll make a note of it and change the applications code when I get a minute.

Thanks again.
Chris.

Alex Feinman said:
ListView is not a databound control. It has no DataSource property

I suspect what you currently have in your code is something like this:

foreach( DataRow row in table.Rows)
{
ListViewItem item = new ListViewItem(row["field1"].ToString());
item.SubItems.Add ...
}

You want to replace it with

foreach( DataRowView rw in table.DefaultView )
{
DataRow row = rw.Row;
ListViewItem item = new ListViewItem(row["field1"].ToString());
item.SubItems.Add ...
}


--
Alex Feinman
---
Visit http://www.opennetcf.org
Chris Podmore said:
Alex,

Thanks for the reply.

The ListView I am populating is bound to the DataSource. The person who
originally wrote the code intended it that way (not sure why and he can't
remember). I have reworked their code and I have added some code to sort the
ListView, this is sufficient for our requirements. I don't have time to
recode it to use a DataSource.
Thanks for your help.
Chris.
 
Back
Top