GridView LinqDataSource Sorting

C

Chuck P

I don't know how the GridView does automagic sorting when using the
LinqDataSource.

I would like to do two things.

1. Set the inital sort order (State ASC)
2. Modify the sort when someone clicks on a colum
(e.g. someone clicks on state, set the sort to State DESC, Town ASC)


I can do 1. by
PageLoad()
if (!IsPostBack) GridView_Ex1.Sort("State",SortDirection.Ascending);
but I don't know if that is the best way.

I looked at LinqDataSource1_Selecting, but didn't see anything in the
OrderBy parameter collection even when I was doing a column sort.

Also I don't know how to do number 2.
 
S

Steven Cheng

Hi Chuck,

As for GridView, the automatic sorting has encapsulated the internal code
logic. For your scenario, if you want to set initial sorting or
programmatically adjust the sorting later, you can consider call the
GridView.Sort method in GridView's PreRender event.

The reason we use "PreRender" event is because at that time, the GridView
has finished all other processing(such as the data loading or default
sorting behavior) and you can do your own customization(and the change will
be stored into viewstate). here is a simple example:

#set initial sorting(when not postbacak) for the last column in GridView
==============
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.Sort(GridView1.Columns[5].SortExpression,
SortDirection.Descending);
}
}
===========

here is a good blog article about using LINQ DataSource and Gridview on
scottgu's weblog:

#LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding
-ui-using-the-asp-linqdatasource-control.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
C

Chuck P

Thanks, I had previously read that but it didn't cover what I was interested
in doing.
I need to set the initial sort and manipulate the sort.
 
C

Chuck P

"Steven Cheng" said:
Hi Chuck,

As for GridView, the automatic sorting has encapsulated the internal code
logic. For your scenario, if you want to set initial sorting or
programmatically adjust the sorting later, you can consider call the
GridView.Sort method in GridView's PreRender event.

The reason we use "PreRender" event is because at that time, the GridView
has finished all other processing(such as the data loading or default
sorting behavior) and you can do your own customization(and the change will
be stored into viewstate). here is a simple example:

#set initial sorting(when not postbacak) for the last column in GridView
==============
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.Sort(GridView1.Columns[5].SortExpression,
SortDirection.Descending);
}
}
===========

here is a good blog article about using LINQ DataSource and Gridview on
scottgu's weblog:

#LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding
-ui-using-the-asp-linqdatasource-control.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
Thread-Topic: GridView LinqDataSource Sorting
thread-index: Ach5lrhidD2TpHnTTzSEsurxCvFMTA==
X-WBNR-Posting-Host: 128.165.159.58
From: =?Utf-8?B?Q2h1Y2sgUA==?= <[email protected]>
Subject: GridView LinqDataSource Sorting
Date: Wed, 27 Feb 2008 15:16:02 -0800
I don't know how the GridView does automagic sorting when using the
LinqDataSource.

I would like to do two things.

1. Set the inital sort order (State ASC)
2. Modify the sort when someone clicks on a colum
(e.g. someone clicks on state, set the sort to State DESC, Town ASC)


I can do 1. by
PageLoad()
if (!IsPostBack) GridView_Ex1.Sort("State",SortDirection.Ascending);
but I don't know if that is the best way.

I looked at LinqDataSource1_Selecting, but didn't see anything in the
OrderBy parameter collection even when I was doing a column sort.

Also I don't know how to do number 2.
 
S

Steven Cheng

Thanks for your reply,

Yes, that article haven't covered the exact sorting customization but does
provide many examples working with LinqDataSource. actually, the setting
initial sorting state(or programmatically set sort state) is the same as
when you use normal DataSource(such as SqlDataSource). If there is anything
else we can help, welcome to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


-------------------
From: =?Utf-8?B?Q2h1Y2sgUA==?= <[email protected]>
References: <[email protected]>
 
C

Chuck P

Is their some place I can hook into so I can
Modify the sort when someone clicks on a column
(e.g. someone clicks on state, set the sort to State DESC, Town ASC)

I don't think I could do this in the grid since it only does one column.
Somewhere in the Linq datasource events?
When does the LINQ OrderBy parameter collection get filled?
It was empty when I looked at the selecting event.
 
S

Steven Cheng

Hi Chuck,

Thanks for your reply. Yes, the data querying is performed in
Datasourcecontrol, However, most sorting behavior(such as sorting
expression and direction) is exposed in GridView control(for you to
customize it). In the following msdn reference, it has explained how the
sorting process works:

#Sorting Data in a GridView Web Server Control
http://msdn2.microsoft.com/en-us/library/hwf94875.aspx

You can use the Sorting event of Gridview to inject your own custom sort
expression. Or you can event turn off default sorting and call the sort
method in your own postback event(raised by certain controls in gridview):

#GridView..::.Sort Method
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.
sort.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.








--------------------
From: =?Utf-8?B?Q2h1Y2sgUA==?= <[email protected]>
References: <[email protected]>
<[email protected]>
 
C

Chuck P

Steven,

Those articles didn't help.
The first one was just generic sorting stuff for someone who has never used
asp.net before. The second shows the default gridview behavior which lets
you set mulitple column sort but not sort order for each column like I need
to do.

That's why I was interested in knowing how the gridview does its sorting
with the LINQ datasource. I was hoping to maybe use the Linq datasource
events?
I was wondering when does the LINQ OrderBy parameter collection get filled?
It was empty when I looked at the selecting event.
 
S

Steven Cheng

Thanks for your reply Chuck,

So the multi-column sorting in your scenario will require different sorting
order for each column? If so, I agree that the built-in GridView sort
doesn't support this(only one sorting direction is used). Also, for such
scenario, I don't think LinqDataSource control will help more because
LinqDataSource control implement the same interfaces as other DataSource
controls. And those DataBound controls such as GridView/DataGrid will
interact with LinqDatasource control through the IDataSource interface
which just accept parameters from Gridview and return result set(for
sorting, it only accept the format of the one GridView supported).

Therefore, if you need more complex sorting logic, I think the reasonable
approach is turn of default sorting and manually do the sorting at data
access layer. I think you can consider using ObjectDataSource instead of
LinqDataSource here since you need more customization at the DataSource
side. You can create a wrapper class that use the Linq context class
internally and implement your sorting code logic there.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
C

Chuck P

Steven, came up with a solution.

The trick is to not let the LinqDataSource grabs the sort column from the
Gridview and to then feed the columns to the LinqDataSource.

This code appends the sort column clicked with the Date field descending,
provided the date column wasn't clicked itself..

First
Set LinqDataSource.AutoSort = false // as you suggested
Gridview allow sorting = true;


protected void LinqDataSource1_Selecting(object sender,
LinqDataSourceSelectEventArgs e)
{

const string EventDateSort = "EventDate desc";

if (string.IsNullOrEmpty(GridView_Ex1.SortExpression))
LinqDataSource1.OrderBy = EventDateSort;
else if (GridView_Ex1.SortExpression== "EventDate")
LinqDataSource1.OrderBy = GridView_Ex1.SortExpression + " " +
GridView_Ex1.SortDirection.ToString();
else
LinqDataSource1.OrderBy = GridView_Ex1.SortExpression + " " +
GridView_Ex1.SortDirection.ToString() + ", " + EventDateSort;
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top