sorting in data grid run-time error: Cannot find table 0.

Discussion in 'ASP .Net Web Controls' started by Matthew Louden, Nov 18, 2003.

  1. I am trying to implement the sort event in data grid. I set the data grid's
    AllowSorting property = True. I implement the following code that should be
    the event will fire when the user click any of the columns in the table:

    Private Sub dgReport1_SortCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    dgReport1.SortCommand
    Dim sortOrder As String = e.SortExpression.ToString()
    Response.Write(sortOrder)
    Dim ds As New DataSet
    Dim dt As DataTable = ds.Tables(0)
    dt.DefaultView.Sort = sortOrder
    dgReport1.DataSource = ds.Tables(0).DefaultView
    dgReport1.DataBind()
    End Sub


    When I click any of the columns in the table, it throw the following run
    time error on line: Dim dt As DataTable = ds.Tables(0).

    Run-time error
    ============
    Cannot find table 0.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.IndexOutOfRangeException: Cannot find table 0.


    I have no idea why table 0 has problem. Because I am able to get the table
    as folows:

    Dim dt As DataTable = ds.Tables(0)
    dgReport1.DataSource = dt
    dgReport1.DataBind()



    Please advise! Thanks !!
    Matthew Louden, Nov 18, 2003
    #1
    1. Advertising

  2. Sure. But does the DataTable exist when you return to the form to sort it? I
    doubt it. The DataTable is not persisted between invocations unless you
    persist it. One approach is to save it to the Session state. When you return
    on Postback, you'll need to rehydrate it.

    --
    ____________________________________
    William (Bill) Vaughn
    Author, Mentor, Consultant
    MVP, hRD
    www.betav.com
    Please reply only to the newsgroup so that others can benefit.
    This posting is provided "AS IS" with no warranties, and confers no rights.
    __________________________________

    "Matthew Louden" <> wrote in message
    news:%23F2$X%...
    > I am trying to implement the sort event in data grid. I set the data

    grid's
    > AllowSorting property = True. I implement the following code that should

    be
    > the event will fire when the user click any of the columns in the table:
    >
    > Private Sub dgReport1_SortCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    > dgReport1.SortCommand
    > Dim sortOrder As String = e.SortExpression.ToString()
    > Response.Write(sortOrder)
    > Dim ds As New DataSet
    > Dim dt As DataTable = ds.Tables(0)
    > dt.DefaultView.Sort = sortOrder
    > dgReport1.DataSource = ds.Tables(0).DefaultView
    > dgReport1.DataBind()
    > End Sub
    >
    >
    > When I click any of the columns in the table, it throw the following run
    > time error on line: Dim dt As DataTable = ds.Tables(0).
    >
    > Run-time error
    > ============
    > Cannot find table 0.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.IndexOutOfRangeException: Cannot find table 0.
    >
    >
    > I have no idea why table 0 has problem. Because I am able to get the table
    > as folows:
    >
    > Dim dt As DataTable = ds.Tables(0)
    > dgReport1.DataSource = dt
    > dgReport1.DataBind()
    >
    >
    >
    > Please advise! Thanks !!
    >
    >
    >
    William \(Bill\) Vaughn, Nov 18, 2003
    #2
    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. Matthew Louden
    Replies:
    0
    Views:
    374
    Matthew Louden
    Nov 18, 2003
  2. Matthew Louden
    Replies:
    1
    Views:
    2,779
    William \(Bill\) Vaughn
    Nov 18, 2003
  3. flamesrock
    Replies:
    8
    Views:
    451
    Hendrik van Rooyen
    Nov 24, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    482
    Pierre Yves
    Jan 10, 2008
  5. Mark

    Data Grid Template Columns at Run Time

    Mark, Sep 25, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    208
Loading...

Share This Page