473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 11088
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Arjen | last post by:
Hi, I get this error message when sorting a gridview: The GridView 'GridView1' fired event Sorting which wasn't handled What do I need to do? Thanks!
4
by: samb | last post by:
When I use manual databinding to a GridView control, as bellow. 'Retrive a DataSet from database Dim ds As DataSet = uda.GetUsers(conectionString) 'gvUsers - The GridView gvUsers.DataSource...
4
by: kurt sune | last post by:
I have a an aspx page with a gridview. The gridview is data bound to a generic list of custom classes. The gridview's DataSource is thus not set. Now I want to add sorting to it. So I create...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
2
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string");...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
3
by: Nathan Sokalski | last post by:
I have a GridView control with three columns, all BoundField columns. They all have a HeaderText and DataField property set, and the third one has a DataFormatString property as well. When I run my...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.