Datagrid Tip of the Day: ASP.NET Datagrids and ViewState
I’ve answered this twice in the last 24 hours, so I figure it’s worthy of a blog post. If you disable ViewState on a page with an ASP.NET Datagrid, and you want to use events—sorting, editing, or paging—then you need to .DataBind() the grid on every Page_Load. Take out that if (!IsPostBack) or If Not IsPostBack Then line that you’ve seen in the examples, and normally is appropriate on a page that uses ViewState. Now what this means is that you end up databinding twice, so make sure to save off your DataSource to a page-level variable so that you don’t retrieve the data twice.
I mention this technique in my MSDN article, Common Datagrid Mistakes. Maybe one day I’ll do a whole article exclusively on Datagrids and ViewState.
--Datagrid Girl