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()

Answers


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.


Need Your Help

Play User's Songs using Silverlight

c# silverlight visual-studio-2010 audio expression-encoder

Does anyone have a working (tested) example of code to play an audio file from isolated storage. The code I currently have, which doesn't throw an exception or make any sound, is:

Android UI design for multiple size factors

android android-layout android-ui

I'm building an app, which starts with a PIN screen similar to the stock PIN entry screen, the problem is that I cannot get the design to work on all kinds of phones, for simplicity's sake let's t...

Issues with jQuery and TinyMCE rich text editor?

jquery tinymce

I'm experimenting with the Tiny MCE editor. We also use jQuery, and I noticed that the standard Tiny MCE install includes a file called tiny_mce_jquery.js. Can anyone enlighten me as to what that's...