Gridview Sorting and Paging problem

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

    Gridview Sorting and Paging problem

    In my web design, I display records from a SQL DataBase according to
    some filters
    the GridView is defined with select commands and selest parameters

    however, I also want to display all records in the database when the
    user press a "View All" button.

    In this case, I need to change the select comand and clear the
    parameters. And I did this in the button_click() event. the GridView
    can display desired results when I press the "View All" button,

    However, when I click the header to sort, the GridView gone away, like
    redered without any data. Press the button again, can show, but not
    sorted.

    i think it is because of the select command.

    when sorting, the SqlDataSource.S elect command is the one I
    declaratively defined one, not the new one I assigned in the
    button_Click() event.

    So what Can I do with this to make the select command changed to the
    one i want?

  • Aytaç ÖZAY

    #2
    Re: Gridview Sorting and Paging problem

    Hi,

    I can't see your code so I suggest you an advise for this situation. The
    best way for doing this is using GridView's paging and sorting mechanisms
    but giving a data source is in code behind files. If you make like this you
    control your code easily and not having problems like this. Also I suggest
    you to use a DataView objects for Data Source. If you don't know how to do
    it as again and I will post a code.

    Have a nice work,

    Aytaç ÖZAY
    Software Developer

    "yefei" <wenjingwishbes t@hotmail.com> wrote in message
    news:1144643577 .745435.252990@ v46g2000cwv.goo glegroups.com.. .[color=blue]
    > In my web design, I display records from a SQL DataBase according to
    > some filters
    > the GridView is defined with select commands and selest parameters
    >
    > however, I also want to display all records in the database when the
    > user press a "View All" button.
    >
    > In this case, I need to change the select comand and clear the
    > parameters. And I did this in the button_click() event. the GridView
    > can display desired results when I press the "View All" button,
    >
    > However, when I click the header to sort, the GridView gone away, like
    > redered without any data. Press the button again, can show, but not
    > sorted.
    >
    > i think it is because of the select command.
    >
    > when sorting, the SqlDataSource.S elect command is the one I
    > declaratively defined one, not the new one I assigned in the
    > button_Click() event.
    >
    > So what Can I do with this to make the select command changed to the
    > one i want?
    >[/color]


    Comment

    • yefei

      #3
      Re: Gridview Sorting and Paging problem

      Thanks very much
      i try to define a gridview and a SqlDataSource without connectionstrin g
      and select command like this
      <asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue"
      AllowSorting="T rue"
      CellPadding="4" DataSourceID="S qlDataSource1"
      ForeColor="#333 333" GridLines="None " EmptyDataText=" No Data Record" >
      <FooterStyle BackColor="#990 000" Font-Bold="True"
      ForeColor="Whit e" />
      <RowStyle BackColor="#FFF BD6" ForeColor="#333 333" />
      <SelectedRowSty le BackColor="#FFC C66" Font-Bold="True"
      ForeColor="Navy " />
      <PagerStyle BackColor="#FFC C66" ForeColor="#333 333"
      HorizontalAlign ="Center" />
      <HeaderStyle BackColor="#990 000" Font-Bold="True"
      ForeColor="Whit e" />
      <AlternatingRow Style BackColor="Whit e" />
      </asp:GridView>
      <asp:Button ID="Button1" runat="server" OnClick="Button 1_Click"
      Text="Button" />
      <asp:SqlDataSou rce ID="SqlDataSour ce1"
      runat="server"> </asp:SqlDataSour ce>

      and a little operation in the .cs file
      protected void Button1_Click(o bject sender, EventArgs e)
      {
      SqlDataSource1. ConnectionStrin g =
      ConfigurationMa nager.Connectio nStrings["ServiceConnect ionString"].ToString();
      SqlDataSource1. SelectCommand = "select * from [tblService]";
      }

      it displays the gridview when i click the button. However, when I click
      the column header or the page index to change, the gridview displays,
      and the "No Data Record" is displayed, like no data in the data source.

      I also tried to define the gridview with connection string and select
      command,. but change the select command when the button is clicked. The
      same problem came out.

      I wrote a line to write the select commad when in Sorting or
      PageIndexChangi ng event, it display the original one defined in the
      ..aspx file, instead of the one changed to in .aspx.cs file in the
      Button_Click() event.

      what could be the reason for this?
      I am very appreciated for your help.

      Comment

      • yefei

        #4
        Re: Gridview Sorting and Paging problem

        the one i defined above, i enabled the sorting and paging but not
        define the sort expression explicitly in the code file

        Comment

        • yefei

          #5
          Re: Gridview Sorting and Paging problem

          i found out that no matter how you define the gridview, when you change
          the select command in the event of some controls, when sorting and
          pageIndexChangi ng, the gridview is load again, and the select command
          is set back to the original one.

          Comment

          • Aytaç ÖZAY

            #6
            Re: Gridview Sorting and Paging problem

            Hi,

            Maybe your SqlDataSource can not bound to your GridView when you click a
            column header or a page index. I advise you not to to use a Toolbox
            Components for this action, if you coded this, you have no problem occured I
            think.

            Have a good work,

            Aytaç ÖZAY
            Software Engineer

            "yefei" <wenjingwishbes t@hotmail.com> wrote in message
            news:1144719998 .491306.181400@ z34g2000cwc.goo glegroups.com.. .[color=blue]
            > Thanks very much
            > i try to define a gridview and a SqlDataSource without connectionstrin g
            > and select command like this
            > <asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue"
            > AllowSorting="T rue"
            > CellPadding="4" DataSourceID="S qlDataSource1"
            > ForeColor="#333 333" GridLines="None " EmptyDataText=" No Data Record" >
            > <FooterStyle BackColor="#990 000" Font-Bold="True"
            > ForeColor="Whit e" />
            > <RowStyle BackColor="#FFF BD6" ForeColor="#333 333" />
            > <SelectedRowSty le BackColor="#FFC C66" Font-Bold="True"
            > ForeColor="Navy " />
            > <PagerStyle BackColor="#FFC C66" ForeColor="#333 333"
            > HorizontalAlign ="Center" />
            > <HeaderStyle BackColor="#990 000" Font-Bold="True"
            > ForeColor="Whit e" />
            > <AlternatingRow Style BackColor="Whit e" />
            > </asp:GridView>
            > <asp:Button ID="Button1" runat="server" OnClick="Button 1_Click"
            > Text="Button" />
            > <asp:SqlDataSou rce ID="SqlDataSour ce1"
            > runat="server"> </asp:SqlDataSour ce>
            >
            > and a little operation in the .cs file
            > protected void Button1_Click(o bject sender, EventArgs e)
            > {
            > SqlDataSource1. ConnectionStrin g =
            > ConfigurationMa nager.Connectio nStrings["ServiceConnect ionString"].ToString();
            > SqlDataSource1. SelectCommand = "select * from [tblService]";
            > }
            >
            > it displays the gridview when i click the button. However, when I click
            > the column header or the page index to change, the gridview displays,
            > and the "No Data Record" is displayed, like no data in the data source.
            >
            > I also tried to define the gridview with connection string and select
            > command,. but change the select command when the button is clicked. The
            > same problem came out.
            >
            > I wrote a line to write the select commad when in Sorting or
            > PageIndexChangi ng event, it display the original one defined in the
            > .aspx file, instead of the one changed to in .aspx.cs file in the
            > Button_Click() event.
            >
            > what could be the reason for this?
            > I am very appreciated for your help.
            >[/color]










            Comment

            Working...