Sorting Gridview in Visual Basic
I am trying to sort a gridview in visual basic using the following method, however the second line of code (Dim dv as DataView...) throws a NullReferenceException. When I debug the code and step through it, I can see that the DataTable dt has a value of "Nothing" after it is instantiated. Any help would be greatly appreciated.
Dim dt As DataTable = grdExpProd.DataSource Dim dv As DataView = dt.DefaultView If (blnExTraining) Then dv.Sort = "ExpirationDate DESC" Else dv.Sort = "ExpirationDate ASC" End If grdExpProd.DataSource = dv grdExpProd.DataBind()
After postback the DataSource of any Web-Databound-Control like GridView is Nothing (null in C#). Note that HTTP is stateless, all objects that are created in a page's life-cycle will be destroyed as soon as the page was rendered.
So the solution is easy, select the DataSource again with the correct ORDER BY.