Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:26:41+00:00 2026-06-12T05:26:41+00:00

By using sample code as a guide, I managed to slap together some code

  • 0

By using sample code as a guide, I managed to slap together some code that will perform sorting and paging on a gridview. However, I’m relatively new to webpage programming so I used ViewState quite often (which from what I’ve gathered, is BAD). As a result, I was wondering if there’s anyway to make my code more efficient? (Or different ways of accomplishing the same thing?)

Front end:

<asp:GridView ID="UserAccounts" runat="server" AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true"
    OnSorting="gridView_Sorting" OnPageIndexChanging="gridView_PageIndexChanging" PageSize = "20">
    <Columns>
        <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:BoundField DataField="Roles" HeaderText="Role" SortExpression="Roles" />
        <asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked Out?" SortExpression="IsLockedOut" />
        <asp:CheckBoxField DataField="IsOnline" HeaderText="Online?" SortExpression="IsOnline" />
        <asp:BoundField DataField="LastLoginDate" HeaderText="Last Login Date" SortExpression="LastLoginDate" />
        <asp:HyperLinkField Text="Manage" DataNavigateUrlFields="UserName" DataNavigateUrlFormatString="ManageDetails.aspx?user={0}" />
    </Columns>
</asp:GridView>

Code Behind to generate table:

private void BindUserAccounts()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("UserName");
        dt.Columns.Add("Email");
        dt.Columns.Add("Roles");
        dt.Columns.Add("IsLockedOut");
        dt.Columns.Add("IsOnline");
        dt.Columns.Add("LastLoginDate");

        var userRoles = from MembershipUser user in Membership.FindUsersByName(this.UsernameToMatch + "%")
                        let roles = Roles.GetRolesForUser(user.UserName)
                        select new
                        {
                            UserName = user.UserName,
                            Email = user.Email,
                            Roles = string.Join(", ", roles),
                            IsLockedOut = user.IsLockedOut,
                            IsOnline = user.IsOnline,
                            LastLoginDate = user.LastLoginDate
                        };

        foreach (var u in userRoles)
        {
            DataRow dr = dt.NewRow();
            dr["UserName"] = u.UserName;
            dr["Email"] = u.Email;
            dr["Roles"] = u.Roles;
            dr["IsLockedOut"] = u.IsLockedOut;
            dr["IsOnline"] = u.IsOnline;
            dr["LastLoginDate"] = u.LastLoginDate;

            dt.Rows.Add(dr);
        }

        UserAccounts.DataSource = dt;
        UserAccounts.DataBind();
        ViewState["DataSource"] = dt;
}

To enable sorting and paging:

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        UserAccounts.PageIndex = e.NewPageIndex;
        BindUserAccounts();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = (DataTable)ViewState["DataSource"];

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            if ((string)ViewState["SortDir"] == "ASC" || String.IsNullOrEmpty((string)ViewState["SortDir"]))
            {
                dataView.Sort = e.SortExpression + " ASC";
                ViewState["SortDir"] = "DESC";
            }
            else if ((string)ViewState["SortDir"] == "DESC")
            {
                dataView.Sort = e.SortExpression + " DESC";
                ViewState["SortDir"] = "ASC";
            }

            UserAccounts.DataSource = dataView;
            UserAccounts.DataBind();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:26:42+00:00Added an answer on June 12, 2026 at 5:26 am

    The code you posted is as efficient as it can be due to the way that you are binding the data to the GridView. More efficient ways can be achieved but not without changing the data binding logic.

    For example, a more efficient way to do this could be to handle paging and sorting on the client side using jQuery+dataTables. Another way that would require less code from your part can be accomplished using SqlDataSource and setting it as the DataSource for the GridView – you wouldn’t have to do the resorting/paging in code. But again, both approaches require significant changes. Your code is as efficient as it can be IMO.

    Update:
    Tim made a good point on his comment – don’t persist the data table on ViewState. Either put it in Session (consider the size of the data before starting to put things in Session) or simply ask the DB to send the data again. Adding this DataTable to ViewState will considerably increase the page size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a few properties that I'm using in some sample code I'm
I'm using SS 2005 if that I've seen sample code like DECLARE @restore =
I'm trying to execute some sample code from the Thrust Quick Start Guide. It's
I'm reading this google guide and using this sample code provided by google ,
I have some sample code which is using factories. I'd like to clean up
I've been using the sample code from this d3 project to learn how to
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I'm working through Beej's socket tutorial . Using the sample code I've created a
I am using something like the above sample code but when i try to
Does anyone know if there is an sample code for using Visual Basic to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.