Sorting issues when reusing a gridView with multiple queries

Last post 11-23-2006 10:59 PM by pbrashear. 3 replies.

Sort Posts:

  • Sorting issues when reusing a gridView with multiple queries

    10-14-2006, 7:31 PM
    • Loading...
    • pbrashear
    • Joined on 10-14-2006, 6:36 PM
    • Posts 3

    I am using a gridview object on my page to display any number of reports and formats based on user-selected options. A user might run several different report formats on a single visit to the page.  The gridview allows autosorting.

    If the user sorts on a field, then later switches to a report format that does not contain that same field name, the following error message is raised at System.Data.DataTable.ParseSortString(String sortString) on the the grid binds:

       Source: System.Data, Cannot find column columnname

    This is happening on the databind method for the gridview after a new SelectCommand is fed to the dataSource.  I can test the sortExpression for the gridview prior to binding, but it is a read only property.  In short, I have a new dataset being fed to the gridView, but relic of a prior sort, that I can not seem to rid myself of, is causing my bind to fail.

    Switching between report views works fine if no user sorting has been done or it has been done on a field that is common between the first and second report formats.

    Any suggestions appreciated!

  • Re: Sorting issues when reusing a gridView with multiple queries

    10-27-2006, 2:36 PM
    • Loading...
    • seanpkelly
    • Joined on 10-27-2006, 6:03 PM
    • Posts 3

    I had the exact same thing happen doing pretty much the same thing of dynamically assigning the SELECT command to a SqlDataSource bound to a GridView component.  I have a dropdown of table names that when selected generates a dynamically bound grid of that database table.  I found the method: 

    GridView1.Sort("", SortDirection.Ascending);

     Which, of course, unnecessarily does the sort again (this is called prior to reassigning the new SELECT command) but it does clear the SortExpression which was the same cause of my problem (that it had the last bound table's expression stuck in it).

     Hope this helps.

  • Re: Sorting issues when reusing a gridView with multiple queries

    10-28-2006, 7:07 AM
    • Loading...
    • seanpkelly
    • Joined on 10-27-2006, 6:03 PM
    • Posts 3

    Just for completeness, I posted the code here of how I am using it (with AutoSorting and AutoPaging on):

     

            <asp:DropDownList ID="ddTableList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddTableList_SelectedIndexChanged">
                <asp:ListItem>Table1</asp:ListItem>
                <asp:ListItem>Table2</asp:ListItem>
                <asp:ListItem>Table3</asp:ListItem>
                <asp:ListItem>Table4</asp:ListItem>
    	</asp:DropDownList>    
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataSourceID="SqlDataSource1" AllowPaging="true" AllowSorting="true"
                EmptyDataText="There are no data records to display.">            
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YourDatabaseConnectionString1 %>" EnableViewState="true"
                ProviderName="<%$ ConnectionStrings:YourDatabaseConnectionString1.ProviderName %>" DataSourceMode="DataSet">
            </asp:SqlDataSource>
     
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    ViewState["_currenttable"] = ddTableList.SelectedValue; 
                                         
                //Not persisted on a postback when set programmatically, so must set back to what it was after last time
                SqlDataSource1.SelectCommand = "SELECT * FROM " + (string)(ViewState["_currenttable"]);
            }
    
            
    
            protected void ddTableList_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridView1.Sort("", SortDirection.Ascending);
                SqlDataSource1.SelectCommand="SELECT * FROM " + ddTableList.SelectedValue;
                ViewState["_currenttable"] = ddTableList.SelectedValue;
                
            }
    
                   
        }
     
  • Re: Sorting issues when reusing a gridView with multiple queries

    11-23-2006, 10:59 PM
    • Loading...
    • pbrashear
    • Joined on 10-14-2006, 6:36 PM
    • Posts 3

    Thanks seanpkelly.

    My case was complicated by the fact that all the queries were in the same class of data so if the currently sorted field existed in the new query, I would want it to keep that sort.  It was all student data, so if a user had sorted one set of data by, say, date of birth and the next request included that field, I assumed that sort order was still relevant to the user.

    I was passing a list of displayed fields to my method, so what I wound up doing if there was a sort expression in the gridview, checking the field list for the sort field. If it was on the list, I did nothing. If it was not, I would call gridview.sort("",SortDirection.Ascending).

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter