Gridview sorting event - Gridview Datasource always Null .. why???

Discussion in 'ASP .Net Web Controls' started by jobs, Oct 23, 2006.

  1. jobs

    jobs Guest

    The Grid displays with multiple rows. I select a column to sort of a
    particular field and the below event is fired, but DataTable is Null so
    code is skipped.


    The GridView:

    <asp:GridView ID="ChargeGridView" runat="server"
    AutoGenerateColumns="False" DataKeyNames="RouteCode"
    Width="700px" SkinID="GridView"
    AllowPaging="True" AllowSorting="true"
    OnSorting="ChargeGridView_Sorting" ShowFooter="true"
    PagerSettings-Mode="NumericFirstLast">


    Protected Sub ChargeGridView_Sorting(ByVal sender As Object, ByVal e
    As GridViewSortEventArgs)
    Dim dataTable As DataTable = CType(ChargeGridView.DataSource,
    DataTable)
    If Not (dataTable Is Nothing) Then '****** (THIS IS ALWAYS
    FALSE) ****
    Dim dataView As DataView = New DataView(dataTable)
    dataView.Sort = e.SortExpression + " " + e.SortDirection
    Response.Write(e.SortExpression + " " + e.SortDirection)
    ChargeGridView.DataSource = dataView
    ChargeGridView.DataBind()
    End If
    End Sub

    Thanks.
    jobs, Oct 23, 2006
    #1
    1. Advertising

  2. jobs

    Phil H Guest

    Re: Gridview sorting event - Gridview Datasource always Null .. why???

    jobs wrote:
    > The Grid displays with multiple rows. I select a column to sort of a
    > particular field and the below event is fired, but DataTable is Null so
    > code is skipped.
    >
    >
    > The GridView:
    >
    > <asp:GridView ID="ChargeGridView" runat="server"
    > AutoGenerateColumns="False" DataKeyNames="RouteCode"
    > Width="700px" SkinID="GridView"
    > AllowPaging="True" AllowSorting="true"
    > OnSorting="ChargeGridView_Sorting" ShowFooter="true"
    > PagerSettings-Mode="NumericFirstLast">
    >
    >
    > Protected Sub ChargeGridView_Sorting(ByVal sender As Object, ByVal e
    > As GridViewSortEventArgs)
    > If Not (dataTable Is Nothing) Then '****** (THIS IS ALWAYS
    > FALSE) ****
    > Dim dataView As DataView = New DataView(dataTable)
    > dataView.Sort = e.SortExpression + " " + e.SortDirection
    > Response.Write(e.SortExpression + " " + e.SortDirection)
    > ChargeGridView.DataSource = dataView
    > ChargeGridView.DataBind()
    > End If
    > End Sub
    >
    > Thanks.


    Hi

    The first thing I would question is this line:

    > Dim dataTable As DataTable = CType(ChargeGridView.DataSource,
    > DataTable)


    I notice you set the DataSource property to something substantial
    further down. But what value or type of object will
    ChargeGridView.DataSource be in the line I quote? Does it exist at that
    point?

    I'm afraid I'm also moved to comment on your coding style. Remember VB
    is not case sensitive. Therefore dataTable and DataTable are treated as
    the same. Not a good idea to use type identifiers as variable names.

    Phil Hall
    Phil H, Oct 23, 2006
    #2
    1. Advertising

  3. jobs

    Guest

    Re: Gridview sorting event - Gridview Datasource always Null .. why???

    Hi, I afraid that this line

    Dim dataTable As DataTable = CType(ChargeGridView.DataSource,
    DataTable)

    might not be working because the DataSource property of ChargeGridView
    would be null after the postback since it is not persisted in
    ViewState. Try to use Cache() or Session() to retain the DataSource as
    described in my blog at
    http://alvinzc.blogspot.com/2006/10/tips-and-tricks-on-cloning-data-bound.html.



    Hope this helps...
    , Oct 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,595
    Mike Schilling
    Mar 29, 2006
  2. Thirsty Traveler

    GridView returning null datasource in sort event

    Thirsty Traveler, May 15, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    7,707
    rjl444
    May 16, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    875
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  5. Jason
    Replies:
    0
    Views:
    380
    Jason
    Oct 4, 2006
Loading...

Share This Page