Advertisement

10.17.2007 at 11:48AM PDT, ID: 22899905
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

ASP.NET c# gridview SortExpression SortDirection w/ manually binding data

Tags: gridview, sortexpression
Gridview properties SortExpression and SortDirection not maintaining values when using paging...

1) I am manually binding to a MySQL database
2) Therefore I am manually handling the firing of the Paging and the Sorting events in codebehind.
3) I do a sort on Column.  It sorts fine...
4) I click on say Page 4.. The sorting is completely lost but goes to page 4 of the default sorting (as if the page was loaded the first time and no sort was indicated)

I don't understand why the gridview maintains the PageIndex Value and not the SortExpression and SortDirection values when posting back...For example, if I click on Page 4 and then click a new column to sort.  The GridView.pageIndex holds it value.

But if I click on a column header for sorting and then click on page 4.  The gridview.sortdirection and gridview.sortexpression are reset....

ASPX
[code]
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
        <asp:UpdatePanel ID="upEmployeeList" runat="server" UpdateMode="conditional" EnableViewState="true">
            <ContentTemplate>
            <asp:GridView cellpadding=5 ID="gvEmployeeList" runat="server"
                AutoGenerateColumns="false" PageSize="20" AllowSorting="true" AllowPaging="true"
                OnPageIndexChanging="gvEmployeeList_PageIndexChanging"
                OnSorting="gvEmployeeList_Sorting"
                OnRowCommand="gvEmployeeList_RowCommand"
                EnableViewState="true"
            >
                <Columns>
                    <asp:BoundField DataField="EmpId" HeaderText="Emp ID" SortExpression="EmpId" ReadOnly="true" />
                    <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" ReadOnly="true" />
                    <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" ReadOnly="true" />
                    <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager" ReadOnly="true" />
                    <asp:BoundField DataField="Division" HeaderText="Division" SortExpression="Division" ReadOnly="true" />
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" ReadOnly="true" />
                    <asp:BoundField DataField="Floor" HeaderText="Floor" SortExpression="Floor" ReadOnly="true" />
                    <asp:ButtonField ImageUrl="~/admin/images/grid_edit.gif" CommandName="edit" ButtonType="Image" />
                    <asp:ButtonField ImageUrl="~/admin/images/grid_delete.gif" CommandName="delete" ButtonType="Image" />
                </Columns>
                <HeaderStyle CssClass="gridHeader"  />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Left" VerticalAlign="Middle" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Left" VerticalAlign="Middle" />
            </asp:GridView>            
        </ContentTemplate>
[/code]
c# file
[code]
    private string sSortDirection="ASC";
    private string sSortExpression="EmpId";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvEmployeeList.DataSource = ds_gvEmployeeList();
            gvEmployeeList.DataBind();
        }
    }
    protected DataSet ds_gvEmployeeList()
    {
        string sSQL = "SELECT a.*,  CONCAT(b.Last_Name,', ',b.First_Name) as Manager, d.NAME as Division, c.NAME as Department ";
        sSQL = sSQL + " from employee a ";
        sSQL = sSQL + " LEFT JOIN Employee b ON a.MGRID=b.EmpID ";
        sSQL = sSQL + " LEFT JOIN Department c ON a.DEPTID=c.DEPTID ";
        sSQL = sSQL + " LEFT JOIN Division d ON a.DVSNID=d.DVSNID ";
        sSQL = sSQL + " ORDER BY " + sSortExpression + " " + sSortDirection;

        DataSet dsTemp = new DataSet();
        using (OleDbDataAdapter da_gvEmployeeList = new OleDbDataAdapter(sSQL, sConnect)){
            da_gvEmployeeList.Fill(dsTemp, "EmployeeList");
        }
        return dsTemp;
    }
    protected void gvEmployeeList_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        gvEmployeeList.PageIndex = e.NewPageIndex;
        gvEmployeeList.DataSource = ds_gvEmployeeList();
        gvEmployeeList.DataBind();
    }
    private string GetSortDirection(string sSortDirection){
        switch (sSortDirection)
        {
            case "ASC":
                return "DESC";
            case "DESC":
                return "ASC";
            default:
                return "ASC";
        }
    }
    protected void gvEmployeeList_Sorting(Object sender, GridViewSortEventArgs e)
    {
        sSortDirection = GetSortDirection(e.SortDirection.ToString());
        sSortExpression = e.SortExpression;
        gvEmployeeList.DataSource = ds_gvEmployeeList();
        gvEmployeeList.DataBind();
    }

any help is greatly appreciated!

Yours in deepest gratitude,

Angela Law


Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: angelalaw
Solution Provided By: TheLearnedOne
Participating Experts: 2
Solution Grade: A
Views: 575
Translate:
Loading Advertisement...
10.17.2007 at 11:54AM PDT, ID: 20095983

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.17.2007 at 01:20PM PDT, ID: 20096708

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.17.2007 at 01:22PM PDT, ID: 20096730

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.17.2007 at 01:24PM PDT, ID: 20096750

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.17.2007 at 01:25PM PDT, ID: 20096762

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.17.2007 at 01:26PM PDT, ID: 20096773

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
10.17.2007 at 11:54AM PDT, ID: 20095983

Rank: Genius

It doesn't look like you are persisting those values after post-back.

Bob
Accepted Solution
 
10.17.2007 at 01:20PM PDT, ID: 20096708
Hi bob,

Thanks for answering, I truly appreciate it...I take it you mean storing them in the viewstate or the session state separately?

What I do not understand is that the Gridview.pageindex persists within the control without any need for outside storage elsewhere when doing postbacks.

Why is this not happening for the SortExpression and SortDirection of the grid view.  I did EnableViewState for the gridview.  So shouldn't that persist through that control without the need to store it in an outside location.

I have gotten around the problem by storing it in the ViewState area.  But I really like to understand WHY and if I did something wrong? the gridView.pageindex value persists within the gridview  control on postbacks automatically so why doesn't sortdirection and sortexpression is not persisting in the gridview control.

Anyone have any explanations?
 
10.17.2007 at 01:22PM PDT, ID: 20096730

Rank: Genius

That is just the nature of the beast.

Bob
 
10.17.2007 at 01:24PM PDT, ID: 20096750
Is there a way to know what properties will persist through the control when enableviewstate is set to true?

Many thanks again :)

Angela Law
 
10.17.2007 at 01:25PM PDT, ID: 20096762

Rank: Genius

Angela,

I don't use the GridView anymore, I use the Telerik RadGrid, so it is difficult to answer that question with any certainty.

Bob
 
10.17.2007 at 01:26PM PDT, ID: 20096773
ah okay,

thank you for your time :)

Ange;la
 
 
04.30.2008 at 10:26AM PDT, ID: 21472807
For sorting, you has two options to do

1) You can use Order by in Query SQL. It's what you are doing

2) You also use DataView to sort.
   a) Once you get DataSet from the Query as ds
   b) You do as the following
             DataView dataView = ds.Tables[0].DefaultView;
             string sFilter = ..... / You sort whatever you want
             dataView.RowFilter = sFilter;
           
             // After that, you use DataView to bind to DataGrid or DataView
             dg1.DataSource = dataView;
             dg1.DataBind();  

I hope it's helpful for you.

Regards,

Sin Le
 
 
 
20080236-EE-VQP-29 / EE_QW_1_20070628