472,122 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

GridView sorting

I have a vb.net 2.0 app that is loading a GridView with a DataSource that is
returned from a function.

The definitions in the function are:

Dim ReportDS As DataSet = New DataSet
Dim ReportTable As System.Data.DataTable = New
System.Data.DataTable("SendTo")

The ReportTable is populated row by row by data gotten back from the
Exchange server.

I then set ReportDS.Tables.Add(ReportTable) and return ReportDS

I then set GridView.DataSource = ReportDS()

This displays exactly as I want it to.

The question is how do I enable sorting on the GridView where it will
actually sort? Setting AllowSorting="True" causes a post back, but no
sorting occurs. What is the optimal way to be able to enable sorting here?

Thanks.

Apr 14 '07 #1
4 10967
Hi Gerhard,
When you set the datasource of the GridView to a dataset or a datatable,
actually this is what happens.
Considering your case : if you write this

Dim ReportTable As System.Data.DataTable = New System.Data.DataTable
"SendTo")
GridView.DataSource = ReportTable

this happens
GridView.DataSource = ReportTable.DefaultView

The datasource is actually set to a DataView not a table or DataSet. Paging
and sorting is done on DataView.

To accomplish what you want you can do this :
DataView dv = new DataView(ReportTable);
dv.Sort = " Name ASC"; or dv.Sort= "Country DESC"; etc
GridView.DataSource = dv;

I hope this helps. In case of any difficult feel free to get back.

--
Regards,
Tushar
MCTS - .NET 2.0 Web App
"Gerhard" wrote:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is
returned from a function.

The definitions in the function are:

Dim ReportDS As DataSet = New DataSet
Dim ReportTable As System.Data.DataTable = New
System.Data.DataTable("SendTo")

The ReportTable is populated row by row by data gotten back from the
Exchange server.

I then set ReportDS.Tables.Add(ReportTable) and return ReportDS

I then set GridView.DataSource = ReportDS()

This displays exactly as I want it to.

The question is how do I enable sorting on the GridView where it will
actually sort? Setting AllowSorting="True" causes a post back, but no
sorting occurs. What is the optimal way to be able to enable sorting here?

Thanks.
Apr 15 '07 #2
Thanks. This helped alot!

One last question. Everything is working fine on this, except for the sort
on one column. This column is populate with a Date value as follows:

ReportTable.Columns.Add("Date")

Dim dateReceived As Date = CDate(....)

workRow("Date") = dateReceived

dv = New DataView(ReportTable)
dv.Sort = "Date Desc"

This sorts, but sorts by the string value of the date instead of the date.

How can I get it to sort by date value instead of the string value?

Thanks.

"Tushar" wrote:
Hi Gerhard,
When you set the datasource of the GridView to a dataset or a datatable,
actually this is what happens.
Considering your case : if you write this

Dim ReportTable As System.Data.DataTable = New System.Data.DataTable
"SendTo")
GridView.DataSource = ReportTable

this happens
GridView.DataSource = ReportTable.DefaultView

The datasource is actually set to a DataView not a table or DataSet. Paging
and sorting is done on DataView.

To accomplish what you want you can do this :
DataView dv = new DataView(ReportTable);
dv.Sort = " Name ASC"; or dv.Sort= "Country DESC"; etc
GridView.DataSource = dv;

I hope this helps. In case of any difficult feel free to get back.

--
Regards,
Tushar
MCTS - .NET 2.0 Web App
"Gerhard" wrote:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is
returned from a function.

The definitions in the function are:

Dim ReportDS As DataSet = New DataSet
Dim ReportTable As System.Data.DataTable = New
System.Data.DataTable("SendTo")

The ReportTable is populated row by row by data gotten back from the
Exchange server.

I then set ReportDS.Tables.Add(ReportTable) and return ReportDS

I then set GridView.DataSource = ReportDS()

This displays exactly as I want it to.

The question is how do I enable sorting on the GridView where it will
actually sort? Setting AllowSorting="True" causes a post back, but no
sorting occurs. What is the optimal way to be able to enable sorting here?

Thanks.

Apr 15 '07 #3
Hi Gerhard,

For the further question about the "Date" column, it is caused by the way
you create the "Date" column(as below):

ReportTable.Columns.Add("Date")

The above method will always create a new column in DataTable and set its
datatype to "String". That's why you found that the values in the "Date"
column are sorted as string values. To make them work as DateTime instance,
you need to specify the datatype(as DateTime) when creating the column. e.g.

===============
Dim ReportTable As New DataTable

'specify the type of the column as DateTime

ReportTable.Columns.Add("Date", GetType(DateTime))
===============

Hope this helps.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 16 '07 #4
Thank you :)

"Steven Cheng[MSFT]" wrote:
Hi Gerhard,

For the further question about the "Date" column, it is caused by the way
you create the "Date" column(as below):

ReportTable.Columns.Add("Date")

The above method will always create a new column in DataTable and set its
datatype to "String". That's why you found that the values in the "Date"
column are sorted as string values. To make them work as DateTime instance,
you need to specify the datatype(as DateTime) when creating the column. e.g.

===============
Dim ReportTable As New DataTable

'specify the type of the column as DateTime

ReportTable.Columns.Add("Date", GetType(DateTime))
===============

Hope this helps.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by samb | last post: by
4 posts views Thread by kurt sune | last post: by
reply views Thread by jobo | last post: by
2 posts views Thread by sivagururaja | last post: by
3 posts views Thread by =?Utf-8?B?YmJkb2J1ZGR5?= | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by Sobin Thomas | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.