GridView sorting

2 views
Skip to first unread message

Alfaking

unread,
Nov 4, 2009, 6:16:36 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi all,

I've created on a web page a gridview
-----------------------------
<asp:GridView ID="Elenco" runat="server" AllowSorting="True"
AutoGenerateColumns="False" CssClass="Bordata" CellSpacing="2"
CellPadding="5">
<RowStyle CssClass="TabellaRiga" />
<Columns>
<asp:CommandField ButtonType="Image" EditImageUrl="~/risorse/
modifica.png" HeaderText="Modifica" ShowEditButton="True"/>
<asp:BoundField DataField="Id" HeaderText="Id" Visible="true"
ItemStyle-Width="10px" SortExpression ="Id" >
<ItemStyle Width="10px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="UserId" HeaderText="Utente" SortExpression
="UserId"/>
<asp:BoundField DataField="Cognome" HeaderText="Cognome"
SortExpression ="Cognome"/>
<asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression
="Nome"/>
<asp:BoundField DataField="eMail" HeaderText="E-Mail" SortExpression
="eMail"/>
<asp:TemplateField HeaderText="Elimina">
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" ImageUrl="~/risorse/elimina.png"
CommandName="Delete" AlternateText="Cancella questo record"
OnClientClick="return confirm('Sei sicuro di voler cancellare questo
record?');" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="TabellaTitolo" HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="TabellaRigaAlt" />
</asp:GridView>
-----------------------------
and here I populate this gridview
-----------------------------
Dim DS As DataSet

DS = Ser.getUtenti("U")

If Glb.checkDS(DS, Me.GetType.FullName, vErrDes, vErrSys) > 0 Then

Me.Elenco.DataSource = DS.Tables("Dati")
Me.Elenco.DataBind()

End If
DS = Nothing

Exit Sub
-------------------------------
now, the header labels appears like hyperlinks, but the sorting is not
working.
I guess is because of the DataBinding...

can anyone help me please ?

Paulo Roberto Pellucci

unread,
Nov 4, 2009, 7:42:32 AM11/4/09
to dotnetde...@googlegroups.com
Hello Favia,
 
Take a look at these links:
 
I guess you'll have to do it programatically, so, these links bring you the solutions. Since you're not generating the columns automatically, you have to page and sort by your own.
 
If the links doesnt help you, start a search on google about "gridview sorting programatically".
 
Best regards,
--
Atenciosamente,
Paulo Roberto S. Pellucci

Cerebrus

unread,
Nov 4, 2009, 7:44:41 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Unless I am very mistaken, the GridView itself does not provide
sorting capabilities. On the contrary, it is very adept at taking
advantage of the Datasource's sorting/paging capabilities. Setting the
AllowSorting property will automatically enable sorting only if your
Datasource also provides this capability. Since you are using a
Dataset (instead of a Datasource control), you need to provide custom
sorting logic in the Sorting eventhandler of the GridView.

Paulo Roberto Pellucci

unread,
Nov 4, 2009, 7:51:59 AM11/4/09
to dotnetde...@googlegroups.com
Actually you're 100% correct. Per example, if you associate the datasource with a SqlDataSource object, it will provide you sorting and paging automatically, right?

Cerebrus

unread,
Nov 4, 2009, 8:30:29 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Absolutely!

But since the OP is already binding to a Dataset, he (Alfa-*king*...
so I deduced male gender) needs to handle the GridView_sorting event
and bind the GridView to a sorted version of the Dataset (easily done
by sorting the associated DataView). I would not recommend using a
Datasource unless it's an ObjectDatasource.


On Nov 4, 5:51 pm, Paulo Roberto Pellucci <paulo.pellu...@gmail.com>
wrote:
> Actually you're 100% correct. Per example, if you associate the datasource
> with a SqlDataSource object, it will provide you sorting and paging
> automatically, right?
>

Alfaking

unread,
Nov 4, 2009, 9:01:20 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On 4 Nov, 14:30, Cerebrus <zorg...@sify.com> wrote:

> But since the OP is already binding to a Dataset, he (Alfa-*king*...
> so I deduced male gender)

you're right :)

> needs to handle the GridView_sorting event
> and bind the GridView to a sorted version of the Dataset (easily done
> by sorting the associated DataView). I would not recommend using a
> Datasource unless it's an ObjectDatasource.

And I'm Using a Dataset coming from a sql statement ("Select * from
table" without any order clause)
I mean, if I take a look at the the gridview properties, I see "Allow
sorting" (set to true)
I give the sort expression the same name as the column...
I click on the header's hyperlink and nothing happens !!!
the event that is raised is

Private Sub Elenco_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs) Handles
Elenco.Sorting

where Elenco is my Gridview...

So I guess that the datagrid HAS the sorting capability...am I wrong ?

and why nothing happens ?
should I fill the dataset once again ?

Alfaking

unread,
Nov 4, 2009, 9:35:16 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On 4 Nov, 13:42, Paulo Roberto Pellucci <paulo.pellu...@gmail.com>
wrote:

> I guess you'll have to do it programatically, so, these links bring you the
> solutions. Since you're not generating the columns automatically, you have
> to page and sort by your own.

I'm not interested in paging my gridview...
I just want to sort it ,and I'm still looking for a good way to do
it :-(

> If the links doesnt help you, start a search on google about "gridview
> sorting programatically".

I'll do it right now !
thanks

Cerebrus

unread,
Nov 5, 2009, 4:45:00 AM11/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
As I mentioned, you need to sort the Dataset in the GridView's Sorting
eventhandler (Elenco_Sorting) and then rebind the GridView to this
sorted version.

Show us what code you have in the "Elenco_Sorting" method.

Paulo Roberto Pellucci

unread,
Nov 5, 2009, 6:15:28 AM11/5/09
to dotnetde...@googlegroups.com
I've sent you some links on my first post, they were showing you how to do it programatically.
As Cerebrus said, even though your gridview has a property saying "Allow Sorting", it is directly related to what object you set in gridview datasource property. If the object (which in your case is a dataset) doesnt allow sorting, the gridview nothing will only allow you to make it by yourself, in other words, you have to do it.
 
So again, take a look at those links and you'll see what you have to do.

Alfaking

unread,
Nov 5, 2009, 7:19:55 AM11/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
---------------------------
Private Sub CaricaUtenti()
Dim DS As DataSet

DS = Ser.getUtenti("U")

If Glb.checkDS(DS, Me.GetType.FullName, vErrDes, vErrSys) > 0 Then
dt = DS.Tables("Dati") '************************************THIS
IS NEW !!!!
Me.Elenco.DataSource = DS.Tables("Dati")
Me.Elenco.DataBind()
End If

DS = Nothing
end sub

-------------------------------
Private Sub Elenco_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs) Handles
Elenco.Sorting

Dim expr As String

dv = New DataView(dt)

If Session("Order") = "ASC" Then
direc = "DESC"
Session("Order") = "DESC"
Else
direc = "ASC"
Session("Order") = "ASC"
End If

expr = e.SortExpression & " " & direc

dv.Sort = expr

Elenco.DataSource = dv
Elenco.DataBind()

End Sub
------------------------------------------------

now it works !!!!

BUT, in the page load I have this

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

'If Not Me.IsPostBack Then ******** COMMENT
CaricaUtenti()
'End If ********COMMENT

Exit Sub

End Sub
-----------------------------------------------------------

The thing is that everytime I have to pass through CaricaUtenti()
(that is the "main" sub) otherwise the sort functionality doesn't
work.
I Think it's not a good solution, because if I have a very big table,
I have to load it again every time I try to sort it...

Any suggestion ?

Alfaking

unread,
Nov 5, 2009, 7:14:21 AM11/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On 5 Nov, 12:15, Paulo Roberto Pellucci <paulo.pellu...@gmail.com>
wrote:
> I've sent you some links on my first post, they were showing you how to do
> it programatically.

yes, I saw them...

> As Cerebrus said, even though your gridview has a property saying "Allow
> Sorting", it is directly related to what object you set in gridview
> datasource property. If the object (which in your case is a dataset) doesnt
> allow sorting, the gridview nothing will only allow you to make it by
> yourself, in other words, you have to do it.

OK, now I understand !!!

>
> So again, take a look at those links and you'll see what you have to do.

done ;-)

eg hff

unread,
Nov 5, 2009, 8:38:17 AM11/5/09
to dotnetde...@googlegroups.com

Cerebrus

unread,
Nov 5, 2009, 12:56:02 PM11/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Couple of ideas to consider:

1. Hit the database to retrieve data only on first load. On subsequent
postbacks (for instance, when a sort is invoked), retrieve the dataset
from Session and manipulate that.

2. Break your CaricaUtenti() method into two logical methods : One
that populates the dataset and the other that Binds data to the
GridView. This way, you can always call the Bind method from other
functions.

3. If you have a large dataset, the only solution is to implement
paging. However, when paging is implemented properly it results in
minimum load on memory, with the obvious tradeoff of more hits to the
database.

On Nov 5, 5:19 pm, Alfaking <favia.alessan...@gmail.com> wrote:
> ---------------------------
> ...

Alfaking

unread,
Nov 6, 2009, 10:58:15 AM11/6/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
maybe I miss something...

> 1. Hit the database to retrieve data only on first load. On subsequent
> postbacks (for instance, when a sort is invoked), retrieve the dataset
> from Session and manipulate that.

how can I retrieve the dataset from Session ?

> 2. Break your CaricaUtenti() method into two logical methods : One
> that populates the dataset and the other that Binds data to the
> GridView. This way, you can always call the Bind method from other
> functions.

yes I can, but the dataset is empty when I go back to that binding
method...

> 3. If you have a large dataset, the only solution is to implement
> paging. However, when paging is implemented properly it results in
> minimum load on memory, with the obvious tradeoff of more hits to the
> database.

one thing at a time....now I'd like to sort, next time maybe i'll try
to page :-)

anyway, the method it's not working, because of that CaricaUtenti()
raised everytime...
I can sort the datagrid, but I loose informations about rows (so I
can't select a single row, or I can't edit-delete it).
OR I can easily use the rowcommand to select one row, but I can't
order the datagrid...

I'm going crazy !!!

Cerebrus

unread,
Nov 6, 2009, 11:47:11 AM11/6/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Maybe the following pseudocode can alleviate that condition! ;-)

---
Dataset Global variable

Page_Load
If Postback
Load Dataset from Session.
Else
Load Dataset from Database.
BindData()
End If
End Page_Load

GridView_Sorting
Sort Dataset object retrieved from Session (in Page_Load).
BindData()
End GridView_Sorting

BindData()
Set GridView Datasource to Dataset.
Bind GridView.
Save this Dataset into Session.
End BindData()
---

Remember to clear the Session object once done with the page. ;-)
Reply all
Reply to author
Forward
0 new messages