Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2004, 09:03 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting a datagrid

is there a way to get a datagrid to sort without having to requery the database again.

I'm new to .NET and would be grateful for any assistance. Full examples would be very useful for me

Many thanks

Thanks for any help provided
 
Old February 23rd, 2004, 12:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The data listing controls in .NET do not provide sorting capabilities. Some do provide a property to indicated what the current sort expression is. However, you must query the data again.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old September 24th, 2004, 02:45 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Friends

Can you give me in simple way the steps to sort a datagrid in asp.net using vb.net.

Thanks
Mike

 
Old September 24th, 2004, 06:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by trekmp
 is there a way to get a datagrid to sort without having to requery the database again.
yes,you can save your DataView as global(put it in Cache or use a static global object)then just work on its Sort property and assign your DataView to grid's DataSource and bind your grid
Quote:
quote:Originally posted by Mike
 Can you give me in simple way the steps to sort a datagrid in asp.net using vb.net.
Have a look at this example(it queries the database in every Page_Load)
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Dim dvwProducts As DataView

Sub Page_Load
  Dim conNorthwind As SqlConnection
  Dim dadProducts As SqlDataAdapter
  Dim dstProducts As DataSet

  ' Create Products DataSet
  dstProducts = New DataSet()
  conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
  dadProducts = New SqlDataAdapter( "Select * From Products", conNorthwind )
  dadProducts.Fill( dstProducts, "Products" )

  ' Create default DataView
  dvwProducts = dstProducts.Tables( "Products" ).DefaultView()

  ' Bind to datagrid
  dgrdProducts.DataSource = dstProducts
  dgrdProducts.DataBind()
End Sub

Sub dgrdProducts_SortCommand( s As Object, e As DataGridSortCommandEventArgs )
  ' Sort DataView
  dvwProducts.Sort = e.SortExpression

  ' Rebind to DataGrid
  dgrdProducts.DataSource = dvwProducts
  dgrdProducts.DataBind()
End Sub
</Script>

<html>
<head><title>SortDataView.aspx</title></head>
<body>

<form Runat="Server">
<asp:DataGrid
  ID="dgrdProducts"
  AllowSorting="True"
  OnSortCommand="dgrdProducts_SortCommand"
  Runat="Server" />
</form>

</body>
</html>
--------------------------------------------
Mehdi.:)
 
Old September 28th, 2004, 03:42 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi

Can you tell me what exact we mean by sorting a datagrid. Why I ask this is coz when I used the commands mentioned, nothing happened, I only had the option to click the columns in a datagrid.

Please help as this is very important. If possible, can you provide me right from the scratch how to sort a datagrid & what does it mean.

Hope you all understand as this is urgent.

Thanks
Mike

 
Old September 28th, 2004, 01:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is Mehdi's example not enough? All of the necessary parts are there.
 
Old September 29th, 2004, 08:40 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

sorting a datagrid means that we want to sort all rows according to
especial column(s)

when you set AllowSorting to true,you can sort your grid
according to the column you click,
(just for more info,you can sort your grid according to more than one column
for example like this: yourDataView.Sort="Column1 ASC,Column2 ASC";
that means if Column1 includes some rows with equal values they will be sorted
according to Column2).

--------------------------------------------
Mehdi.:)
 
Old December 18th, 2004, 06:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi again,
above example sorts a grid but there is also a better way(in most cases),
we could save all datasource to cache then use that instead of connecting to datasource every time there is a request for new sortition ...
see below example(just has a few changes from above example)
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Sub Page_Load
  If Not IsPostBack Then
    ' Bind to datagrid
    dgrdProducts.DataSource = GetProducts()
    dgrdProducts.DataBind()
  End If
End Sub

Function GetProducts() As DataView
  Dim dstProducts As DataSet
  Dim conNorthwind As SqlConnection
  Dim dadProducts As SqlDataAdapter
  Dim dvwProducts As DataView

  dvwProducts = Cache( "Products" )
  If dvwProducts Is Nothing Then
    dstProducts = New DataSet()
    conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
    dadProducts = New SqlDataAdapter( "Select * from Products", conNorthwind )
    dadProducts.Fill( dstProducts, "Products" )
    dvwProducts = dstProducts.Tables( "Products" ).DefaultView()
    Cache( "Products" ) = dvwProducts
  End If
  Return dvwProducts
End Function

Sub dgrdProducts_SortCommand( s As Object, e As DataGridSortCommandEventArgs )
  Dim dvwProducts As DataView

  ' Sort DataView
  dvwProducts = GetProducts()
  dvwProducts.Sort = e.SortExpression

  ' Rebind to DataGrid
  dgrdProducts.DataSource = dvwProducts
  dgrdProducts.DataBind()
End Sub
</Script>

<html>
<head><title>CacheProducts.aspx</title></head>
<body>

<form Runat="Server">
<asp:DataGrid
  ID="dgrdProducts"
  AllowSorting="True"
  OnSortCommand="dgrdProducts_SortCommand"
  Runat="Server" />
</form>
</body>
</html>
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid sorting Amorous ASP.NET 1.x and 2.0 Application Design 1 September 30th, 2005 11:44 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
DataGrid Sorting spm74 ASP.NET 1.0 and 1.1 Basics 3 May 26th, 2004 08:52 AM
Sorting a Datagrid - Help Please! Pauline VS.NET 2002/2003 0 August 29th, 2003 06:20 AM
DataGrid Sorting Example SPRIBob BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 10 July 30th, 2003 09:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.