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 6536133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:27:18+00:00 2026-05-25T10:27:18+00:00

I know that GridView can handle sorting and paging if I bind it to

  • 0

I know that GridView can handle sorting and paging if I bind it to an ObjectDataSource that only needs to get the list of items in a SelectMethod. Since apparently all the ODS is doing is get the items I tried to bind the GridView directly to the collection of items.

I tried:

[aspx]

<asp:Button runat="server" OnClick="ItemsSearch" Text="Search" />
<asp:GridView runat="server" ID="ItemsGV"
    AllowPaging="true" AllowSorting="true" PageSize="4" />

[codebehind]

protected void ItemsSearch(object sender, EventArgs e)
{
    DataSet Items = new DataSet();
    Items.ReadXml(MapPath("Items.xml"));
    Session["items"] = Items;
    ItemsGV.DataSource = Session["items"];
    ItemsGV.DataBind();
}

The GridView is loaded with the data but if I click to sort or change page nothing happens.

Any ideea how to make this kind of binding work?

  • 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-05-25T10:27:18+00:00Added an answer on May 25, 2026 at 10:27 am

    After studying some tutorials I ended up with this solution:

    [Default.aspx]

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TemplateGridViewNoDataSourcePagingSorting.Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
        <asp:Button ID="Button1" runat="server" OnClick="ItemsSearch" Text="Search" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
          <ContentTemplate>
            <asp:GridView runat="server" ID="ItemsGV" Visible="false" 
              AllowPaging="true" AllowSorting="true" PageSize="4"
              OnPageIndexChanging="ItemsGV_PageIndexChanging" OnSorting="ItemsGV_Sorting">
            </asp:GridView>
          </ContentTemplate>
        </asp:UpdatePanel>
      </form>
    </body>
    </html>
    

    [Default.aspx.cs]

    using System;
    using System.Data;
    using System.Threading;
    using System.Web.UI.WebControls;
    
    namespace TemplateGridViewNoDataSourcePagingSorting
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Thread.Sleep(3000); // simulate long response time
                    var ItemsDataSet = new DataSet();
                    ItemsDataSet.ReadXml(MapPath("Items.xml"));
                    Session["items"] = ItemsDataSet.Tables[0];
                    ViewState["sortingOrder"] = string.Empty;
                }
            }
    
            protected void ItemsGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                ItemsGV.PageIndex = e.NewPageIndex;
                ItemsGV.DataSource = Session["items"];
                ItemsGV.DataBind();
            }
    
            protected void ItemsSearch(object sender, EventArgs e)
            {
                ItemsGV.Visible = true;
                DataBindGrid("", "");
            }
    
            private void DataBindGrid(string sortExpr, string sortOrder)
            {
                // if sorting expression had changed
                // set sort order to ascending
                if (sortExpr != (string)ViewState["sortingExpression"])
                {
                    sortOrder = "asc";
                    ViewState["sortingOrder"] = "asc";
                }
                ViewState["sortingExpression"] = sortExpr;
    
                var dt = Session["items"] as DataTable;
                if (dt != null)
                {
                    DataView dv = dt.DefaultView;
                    if (sortExpr != string.Empty)
                        dv.Sort = sortExpr + " " + sortOrder;
                    ItemsGV.DataSource = dv;
                    ItemsGV.DataBind();
                }
                else
                {
                    ItemsGV.DataSource = null;
                    ItemsGV.DataBind();
                }
            }
    
            protected void ItemsGV_Sorting(object sender, GridViewSortEventArgs e)
            {
                DataBindGrid(e.SortExpression, sortingOrder);
            }
    
            public string sortingOrder
            {
                get
                {
                    if (ViewState["sortingOrder"].ToString() == "desc")
                        ViewState["sortingOrder"] = "asc";
                    else
                        ViewState["sortingOrder"] = "desc";
                    return ViewState["sortingOrder"].ToString();
                }
                set
                {
                    ViewState["sortingOrder"] = value;
                }
            }
        }
    }
    

    [Items.xml]

    <?xml version="1.0" encoding="utf-8" ?>
    <Items>
      <Item>
        <ItemName>Item01</ItemName>
        <ItemDescription>Item01 Description</ItemDescription>
        <ItemPrice>2.50</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item02</ItemName>
        <ItemDescription>Item02 Description</ItemDescription>
        <ItemPrice>1.50</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item03</ItemName>
        <ItemDescription>Item03 Description</ItemDescription>
        <ItemPrice>3.50</ItemPrice>
        <ItemInStock>N</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item04</ItemName>
        <ItemDescription>Item04 Description</ItemDescription>
        <ItemPrice>5.00</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item05</ItemName>
        <ItemDescription>Item05 Description</ItemDescription>
        <ItemPrice>5.50</ItemPrice>
        <ItemInStock>N</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item06</ItemName>
        <ItemDescription>Item06 Description</ItemDescription>
        <ItemPrice>4.50</ItemPrice>
        <ItemInStock>N</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item07</ItemName>
        <ItemDescription>Item07 Description</ItemDescription>
        <ItemPrice>1.50</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item08</ItemName>
        <ItemDescription>Item08 Description</ItemDescription>
        <ItemPrice>7.50</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item09</ItemName>
        <ItemDescription>Item09 Description</ItemDescription>
        <ItemPrice>0.50</ItemPrice>
        <ItemInStock>N</ItemInStock>
      </Item>
      <Item>
        <ItemName>Item10</ItemName>
        <ItemDescription>Item10 Description</ItemDescription>
        <ItemPrice>3.50</ItemPrice>
        <ItemInStock>Y</ItemInStock>
      </Item>
    </Items>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can use exclamation sign to bind array of simple types
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that IList is the interface and List is the concrete type but
I'm having trouble creating a Gridview that can span 2 rows for each record.
I am using PageIndexChanging event for handling GridView paging in C#. But don't know
Does any one know of a control that i can use with a ASP.Net
I select a textbox in a gridview row. I know that a gridview is
I have a user control that has a gridview in it with paging. The

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.