Sorting with Datagrid ViewState disabled

I've talked in the past about sorting a Datagrid without ViewState.  The key to getting this to work is calling .DataBind() on the DataGrid during Page_Load in order to wire up the SortCommand event.  I wanted to clarify something though, the .DataBind() doesn't have to be to the *real* .DataSource of the Datagrid, it can just be a dummy value.  Like so:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
   dgReport.DataSource = New DataTable
   dgReport.DataBind()
'bind to dummy datasource to wire up SortCommand event (ViewState is disabled)
End Sub

Private Sub dgReport_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgReport.SortCommand
   ...
   BindData() 'bind to *real* datasource
End Sub

Marcie

Published Monday, August 22, 2005 3:24 PM by datagridgirl

Comments

# re: Sorting with Datagrid ViewState disabled

Marcy,

Why not disable the viewstate on the grid but on each item row.

So for example in the DataGrid1_ItemCreated event you could add the
following line of code:

if (e.Item.ItemType == ListItemType.Item)
e.Item.EnableViewState = false;

The viewstate for the datagrid will be very small and you would still keep sorting, paging and all the other events.

Kind regards,

Marcel

Tuesday, August 23, 2005 4:36 AM by Marcel van den Hof

# re: Sorting with Datagrid ViewState disabled

Looks good - I'll try your way and compare with Marcel's way.

Tuesday, August 23, 2005 11:02 AM by Ben Adams

# re: Sorting with Datagrid ViewState disabled

Excellent Ben,

Let me know what you think. BTW nice website I like the ASCII character.

Regards,

Marcel van den Hof

Tuesday, August 23, 2005 11:07 AM by Marcel van den Hof

# re: Sorting with Datagrid ViewState disabled

Marcel,
Your way works also, both our methods look to be about two lines of code.

Marcie

Tuesday, August 23, 2005 1:33 PM by Marcie

# re: Sorting with Datagrid ViewState disabled

Maybe you should add this to your blog.

I think this solution is an ideal way to retain all the functionality of the datagrid (paging, sorting, etc…) without hacking around with the grid. Obviously rebinding to a datasource is required on postbacks.

Regards,

Marcel van den Hof

Tuesday, August 23, 2005 2:09 PM by Marcel van den Hof

# re: Sorting with Datagrid ViewState disabled

Why not just use the addhandler to hookup?

AddHandler dgReport.SortCommand, AddressOf dgReport_SortCommand

Since this is a dynamic way to hook into
events at runtime and you don't need to use
a dummy object. You would want to check for
postback for only this call to addhandler
so you aren't hooking in too many times.

I use it and it works for me for all sorts of
datagrid events w/Viewstate off.


Tuesday, August 30, 2005 3:30 PM by Rachel

# re: Sorting with Datagrid ViewState disabled

Great idea, Rachel!

Tuesday, August 30, 2005 3:52 PM by Marcie

# re: Sorting with Datagrid ViewState disabled

Hmmm, I liked it, but it didn't work for me.

Tuesday, August 30, 2005 3:58 PM by Marcie

# re: Sorting with Datagrid ViewState disabled

Try this real quick snippet...I didn't do any 2 way sorting or anything but you can use a Sesison var or similar go from there. Hope it works for you.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
binddata("contactname") 'default sort
If Page.IsPostBack Then AddHandler DataGrid1.SortCommand, AddressOf DataGrid1_SortCommand
End Sub

Sub binddata(ByVal SortExp As String)
Dim cn As New SqlConnection("data source=server;user id=user;password=pwd;initial catalog=northwind")
cn.Open()
Dim ds As New DataSet
Dim da As New SqlDataAdapter("select * from customers order by " & SortExp, cn)
da.Fill(ds, "customers")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
binddata(e.SortExpression)
End Sub

Tuesday, August 30, 2005 4:38 PM by Rachel

# re: Sorting with Datagrid ViewState disabled

Hmmm, I think in my case, this doesn't work because my grid is only bound after a postback. (A "Run" button has to be clicked before BindData is ever called, so IsPostBack is always true).

Tuesday, August 30, 2005 5:05 PM by Marcie

# re: Sorting with Datagrid ViewState disabled

Rachel's way will work, but it will bind the data twice when the user chooses a sort.

Marcel's way sounds interesting, but Marcie's way was easier, so I did that. ;)

Saturday, September 03, 2005 11:42 PM by Mark

# re: Sorting with Datagrid ViewState disabled

Marcie's way works for me too. Many thanks

Wednesday, August 22, 2007 6:52 AM by Paul Marshall

# re: Sorting with Datagrid ViewState disabled

For VB.NET 2005, let try this

For Each ccc As DataGridViewColumn In DataGridView1.Columns

           ccc.SortMode = DataGridViewColumnSortMode.NotSortable

       Next

Tuesday, November 27, 2007 4:19 AM by Nin

# re: Sorting with Datagrid ViewState disabled

Hey Marcel,

Without controlling it further didn't you forgot

AlternatingItem ? What about Header and Footer?

Another approach would be OnItemDataBound (from inside a control. Must be something like DataGrid1_ItemDataBound(...) from webpage

regards

Ben

Monday, October 05, 2009 12:14 PM by Ben

# re: Sorting with Datagrid ViewState disabled

423366.. Reposted it :)

Wednesday, May 25, 2011 8:45 AM by weblogs.asp.net

Leave a Comment

(required) 
(required) 
(optional)
(required)