RE: Sorting a Gridview Control / Linq Result Set from SPROC

Tech-Archive recommends: Fix windows errors by optimizing your registry



Ok, thanks. The solution seems fine, but I have to ask: performance wise, is
this a good thing to do?

It seems like we're doing a query and then burning time looping through the
data again after retrieving the result set and then binding. Would it be
better to use something other than LINQ, e.g. just do it the old way: query,
fill the data table, sort the datatable, and bind?

"Allen Chen [MSFT]" wrote:

Hi,

Based on my understanding you're using Linq to Sql and retrieve data via
the stored procedure. You want to sort/paging the GridView. If my
understanding is wrong please correct me.

The simplest solution I can provide is to use ObjectDataSource and return a
DataTable. Thus we can use the built-in sorting/paging that
ObjectDataSource provides. Please try following code:

using System.Collections;
using WebApplication8;
using System.Data.Linq;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
public class MyClass {
public DataTable Select()
{

DataClasses1DataContext context = new DataClasses1DataContext();

ISingleResult<SelectQuery1Result> result =
context.SelectQuery1();//SelectQuery1 is the auto-generated code to call a
stored procedure.It's context.selectCategories() in your case.

DataTable dt = new DataTable();
PropertyInfo[] propertys =
typeof(SelectQuery1Result).GetProperties();//The reason to use reflection
is that we don't have to change code in this BLL if the ORM changes.
foreach (PropertyInfo p in propertys)
{
dt.Columns.Add(p.Name);
}
foreach (SelectQuery1Result r in result)
{
DataRow dr = dt.NewRow();
foreach (PropertyInfo p in propertys)
{
dr[p.Name] = p.GetValue(r, null);

}
dt.Rows.Add(dr);
}
return dt;
}
}
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="Select" TypeName="MyClass"></asp:ObjectDataSource>




<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="True" AllowPaging="true" PageSize="2"
DataSourceID="ObjectDataSource1" >

</asp:GridView>

Please have a try and feel free to let me know if you have any questions.
Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
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: Sorting a Gridview Control / Linq Result Set from SPROC
| thread-index: Acj83DFXc8mly4CMSiuiZOqp7LBz6g==
| X-WBNR-Posting-Host: 207.46.19.168
| From: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ==?=
<checkraisercommunitynospam@xxxxxxxxxxxxxxxxxxxxxxxxx>
| Subject: Sorting a Gridview Control / Linq Result Set from SPROC
| Date: Tue, 12 Aug 2008 17:33:23 -0700
| Lines: 32
| Message-ID: <E73A9864-F8D8-488B-8C72-780311ED4202@xxxxxxxxxxxxx>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:73793
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
|
| Trying to sort a PAGED gridview control by one template column. The
result
| set I get back is from a LINQ stored procedure query. I have the
following
| set on my Gridview Tag: allowpaging="true", AllowSorting="true",
| onpageindexchanging="myGrid_PageIndexChanged", and the
| OnSorting="myGrid_Sorting"
|
| In the myGrid_Sorting event, I have a call the Linq method that runs a
| "select categories" stored proc:
|
| myGrid.DataSource = context.selectCategories();
| myGrid.DataBind();
|
| The query returns three fields in the result set that are used in the
Grid:
| ItemCategoryId, ItemCatgoryDescription, ItemCategoryRecordStatus.
|
| I can't figure out how to order the LINQ results to sort the Gridview by
| ItemCategoryDescription and support the paging in the Gridview.
|
|
|
|
|
|
|
|
|
|
|
|
|
|


.



Relevant Pages

  • RE: Full Text Indexing Performance?
    ... Thank you for using Microsoft MSDN Managed Newsgroup. ... Server you can specify a list of columns in the CONTAINS query. ... Microsoft Online Community Support ...
    (microsoft.public.sqlserver.setup)
  • Re: Excel 2007 data query parameter issue
    ... Welcome to Microsoft Newsgroup Support Service! ... in the Microsoft Query, so our question is: how to add a parameter to the ... This will start up the Query Wizard by default, ...
    (microsoft.public.excel.programming)
  • RE: Central Events for LINQ to SQL?
    ... Welcome to Microsoft MSDN newsgroup! ... LINQ to SQL does not export such a "central event" to retrieve all these ... each entity class has implemented the INotifyPropertyChanging and ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: UDF gives 0 zero records on SS2k; Correct on MSDERelA
    ... Please run the function directly in query design window. ... use SQL profiler on SBS 2003 server to trace the the SQL query. ... Microsoft Online Partner Support ... Microsoft technology partners in the United States and Canada. ...
    (microsoft.public.access.reports)
  • Re: MISSING ACCESS RECORDS
    ... Thanks for using Microsoft Newsgroup Support Service, ... There are 40,000 rows in the query. ... 4.If you can provide the reproducible Access database and sent it to me, ...
    (microsoft.public.dotnet.framework.adonet)