GridView returning null datasource in sort event

Discussion in 'ASP .Net' started by Thirsty Traveler, May 15, 2006.

  1. I have a gridview where the datasource is bound after a selection. The
    result of doing this is that sorting and paging do not work. I was given a
    sample of how to resolve this, however my attempt to explicity enable
    sorting failes because, unlike the sample, my sort event has a null in the
    datasource and I am unsure why this is so.

    Sample HTML code is:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White"
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
    CellPadding="3"
    GridLines="Vertical" AllowPaging="True" AllowSorting="true"
    PagerSettings-Mode="Numeric"
    PagerSettings-Position="Bottom" PagerStyle-HorizontalAlign="Left"
    OnPageIndexChanging="GridView1_PageIndexChanging"
    OnSorting="GridView1_Sorting">
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <Columns>
    <asp:BoundField DataField="Job Name" HeaderText="Job Name"
    ReadOnly="True" SortExpression="Job Name" />
    <asp:BoundField DataField="Agency Number" HeaderText="Agency Number"
    ReadOnly="True" SortExpression="Agency Number" />
    <asp:BoundField DataField="Tracking Number" HeaderText="Tracking
    Number" ReadOnly="True" SortExpression="Tracking Number" />
    <asp:BoundField DataField="Mailing Date" HeaderText="Mailing Date"
    HtmlEncode="False" ReadOnly="True" SortExpression="Mailing Date"
    DataFormatString="{0:MM/dd/yyyy}" />
    </Columns>
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White"
    />
    <PagerStyle BackColor="#999999" ForeColor="Black"
    HorizontalAlign="Center" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="Gainsboro" />
    </asp:GridView>

    The code-behind sort event is:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
    DataTable dataTable = GridView1.DataSource as DataTable; <<<<<<<<<< THIS
    IS NULL EVEN THOUGH SET AFTER A SUBMIT QUERY (below)
    if (dataTable != null)
    {
    int pidx = GridView1.PageIndex;
    string sortDirection = GetSortDirection();
    DataView dataView = new DataView(dataTable);
    dataView.Sort = e.SortExpression + " " + sortDirection;
    GridView1.DataSource = dataView;
    GridView1.DataBind();
    GridView1.PageIndex = pidx;
    }
    }

    The datasource binding occurs after a button click as follows:

    protected void SearchTable_Click(object sender, ImageClickEventArgs e)
    {
    string conn =
    ConfigurationManager.ConnectionStrings["TST"].ConnectionString;
    TSTBLL bll = new TSTBLL();
    TSTMLBE mt = bll.SubmitQuery(tbJobName.Text, tbAgencyNumber.Text,
    tbOrderDateFrom.Text, tbOrderDateTo.Text, conn);
    GridView1.DataSource = mt.Tst_Table;
    GridView1.DataBind();
    }
     
    Thirsty Traveler, May 15, 2006
    #1
    1. Advertising

  2. Thirsty Traveler

    rjl Guest

    i would try not to instantane a new object where you say it is null,
    this is wiping out the data and assinging a new object. perhaps, not
    sure of further details though.
     
    rjl, May 16, 2006
    #2
    1. Advertising

  3. Actually, I was trying to manipulate the datasource that was created with
    the filter request. I finally had to resort to sticking the entire dataset
    into a session object and use that for sorting and paging.

    "rjl" <> wrote in message
    news:...
    >i would try not to instantane a new object where you say it is null,
    > this is wiping out the data and assinging a new object. perhaps, not
    > sure of further details though.
    >
     
    Thirsty Traveler, May 16, 2006
    #3
  4. Thirsty Traveler

    rjl444 Guest

    that is what MSFT would do. I dont like that though, I jsut as soon
    read data again.
     
    rjl444, May 16, 2006
    #4
    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:
    27,589
    Mike Schilling
    Mar 29, 2006
  2. saha
    Replies:
    0
    Views:
    4,994
  3. jason
    Replies:
    13
    Views:
    938
    John B. Matthews
    May 14, 2010
  4. jobs
    Replies:
    2
    Views:
    1,721
  5. Navin
    Replies:
    1
    Views:
    769
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page