GridView returning null datasource in sort event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thirsty Traveler

    GridView returning null datasource in sort event

    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" AutoGenerateCol umns="False"
    BackColor="Whit e"
    BorderColor="#9 99999" BorderStyle="No ne" BorderWidth="1p x"
    CellPadding="3"
    GridLines="Vert ical" AllowPaging="Tr ue" AllowSorting="t rue"
    PagerSettings-Mode="Numeric"
    PagerSettings-Position="Botto m" PagerStyle-HorizontalAlign ="Left"
    OnPageIndexChan ging="GridView1 _PageIndexChang ing"
    OnSorting="Grid View1_Sorting">
    <FooterStyle BackColor="#CCC CCC" ForeColor="Blac k" />
    <Columns>
    <asp:BoundFie ld DataField="Job Name" HeaderText="Job Name"
    ReadOnly="True" SortExpression= "Job Name" />
    <asp:BoundFie ld DataField="Agen cy Number" HeaderText="Age ncy Number"
    ReadOnly="True" SortExpression= "Agency Number" />
    <asp:BoundFie ld DataField="Trac king Number" HeaderText="Tra cking
    Number" ReadOnly="True" SortExpression= "Tracking Number" />
    <asp:BoundFie ld DataField="Mail ing Date" HeaderText="Mai ling Date"
    HtmlEncode="Fal se" ReadOnly="True" SortExpression= "Mailing Date"
    DataFormatStrin g="{0:MM/dd/yyyy}" />
    </Columns>
    <RowStyle BackColor="#EEE EEE" ForeColor="Blac k" />
    <SelectedRowSty le BackColor="#008 A8C" Font-Bold="True" ForeColor="Whit e"
    />
    <PagerStyle BackColor="#999 999" ForeColor="Blac k"
    HorizontalAlign ="Center" />
    <HeaderStyle BackColor="#000 084" Font-Bold="True" ForeColor="Whit e" />
    <AlternatingRow Style BackColor="Gain sboro" />
    </asp:GridView>

    The code-behind sort event is:

    protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
    {
    DataTable dataTable = GridView1.DataS ource as DataTable; <<<<<<<<<< THIS
    IS NULL EVEN THOUGH SET AFTER A SUBMIT QUERY (below)
    if (dataTable != null)
    {
    int pidx = GridView1.PageI ndex;
    string sortDirection = GetSortDirectio n();
    DataView dataView = new DataView(dataTa ble);
    dataView.Sort = e.SortExpressio n + " " + sortDirection;
    GridView1.DataS ource = dataView;
    GridView1.DataB ind();
    GridView1.PageI ndex = pidx;
    }
    }

    The datasource binding occurs after a button click as follows:

    protected void SearchTable_Cli ck(object sender, ImageClickEvent Args e)
    {
    string conn =
    ConfigurationMa nager.Connectio nStrings["TST"].ConnectionStri ng;
    TSTBLL bll = new TSTBLL();
    TSTMLBE mt = bll.SubmitQuery (tbJobName.Text , tbAgencyNumber. Text,
    tbOrderDateFrom .Text, tbOrderDateTo.T ext, conn);
    GridView1.DataS ource = mt.Tst_Table;
    GridView1.DataB ind();
    }


  • rjl

    #2
    Re: GridView returning null datasource in sort event

    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.

    Comment

    • Thirsty Traveler

      #3
      Re: GridView returning null datasource in sort event

      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" <rjl444@hotmail .com> wrote in message
      news:1147752613 .428463.89920@j 55g2000cwa.goog legroups.com...[color=blue]
      >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.
      >[/color]


      Comment

      • rjl444

        #4
        Re: GridView returning null datasource in sort event

        that is what MSFT would do. I dont like that though, I jsut as soon
        read data again.

        Comment

        Working...