DataView.Sort (repost)

G

Guest

I am trying to sort a datatable, and return the sorted
results. Not working with a datagrid, or ASP.NET. Cannot
do the sort on the database.

DataView dv = existing table view
dv.sort = "EVENT_DATE DESC"

DataTable NewTable = new DataTable();
foreach (DataRowView drv in dv)
{
DataRow Row = drv.Row;
NewTable.ImportRow(Row);
}
return NewTable

The result is the same as the existing table. What am I
missing here?

Thanks,

David
 
W

William Ryan

Without calling this method, what happens if you use the Sort on the
DataView where you are making this call from and just bind it to the grid
from there...


I have this proc fire when a user Clicks on a ASP.NET Grid Column (passing
in the column/field name)

I know this is a stupid question, but are you positive that you are binding
to the correct table? Either way, you should be able to just sort it right
before the call to DataSource and Databind and it shoudl work for you.

I've used this methodology many times and not had a problem. Also, you may
want to try Debug.WriteLine in your foreach loop for the time being and
verify that the thing is/is not sorted there before you return the table.
There could be other problems unrelated to the sort that may give it the
appearance that it isn't sorted although it really is. Also, make sure the
grid itself has its AllowSorting property set to true.

Let me know if all of these are already addressed and if so, can you post
the code where you do the Binding, that may provide some insight if none of
the above are the problem.

HTH,

Bill
'sorted data
Protected Sub BindData(ByVal sortField As String)
Try

If cboPages.SelectedItem.Value = "All" Then
dgLogs.AllowPaging = False
Else
dgLogs.AllowPaging = True
dgLogs.PageSize = CType(cboPages.SelectedItem.Value, Int32)
End If
Dim dvw As DataView
dvw = CType(Session("Data_Table"), DataTable).DefaultView
dvw.Sort = sortField
dgLogs.DataSource = dvw
dgLogs.DataBind()

Catch sq As SqlException
Console.Write(sq.ToString)
Catch exc As System.Exception
Console.WriteLine(exc.ToString)
End Try

End Sub
 
G

Guest

Not using a DataGrid. Not using ASP.NET. This is a
middle-tier component that needs to do a sort on the
dataset before returning to the client.

I can see that in ASP.NET, it is as easy as binding the
grid to the dataview. I don't have that luxury.

BTW, if instead of building another table, I print out the
values as I loop through the dataview, the order is what I
expected.

Thanks,

David

-----Original Message-----
Without calling this method, what happens if you use the Sort on the
DataView where you are making this call from and just bind it to the grid
from there...


I have this proc fire when a user Clicks on a ASP.NET Grid Column (passing
in the column/field name)

I know this is a stupid question, but are you positive that you are binding
to the correct table? Either way, you should be able to just sort it right
before the call to DataSource and Databind and it shoudl work for you.

I've used this methodology many times and not had a problem. Also, you may
want to try Debug.WriteLine in your foreach loop for the time being and
verify that the thing is/is not sorted there before you return the table.
There could be other problems unrelated to the sort that may give it the
appearance that it isn't sorted although it really is. Also, make sure the
grid itself has its AllowSorting property set to true.

Let me know if all of these are already addressed and if so, can you post
the code where you do the Binding, that may provide some insight if none of
the above are the problem.

HTH,

Bill
'sorted data
Protected Sub BindData(ByVal sortField As String)
Try

If cboPages.SelectedItem.Value = "All" Then
dgLogs.AllowPaging = False
Else
dgLogs.AllowPaging = True
dgLogs.PageSize = CType
(cboPages.SelectedItem.Value, Int32)
 

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