G
Guest
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current page.
i.e.
If I have:
IntegerValue StringValue CurrencyValue
0 Item 0 0
1 Item 1 1.23
< >
then sort I will get:
IntegerValue StringValue CurrencyValue
8 Item 8 9.84
7 Item 7 8.61
< >
and what I want is:
IntegerValue StringValue CurrencyValue
1 Item 1 1.23
0 Item 0 0
< >
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
private void Page_Load(object sender, System.EventArgs e)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
ICollection CreateDataSource()
{
// Create a Random object to mix up the order of items in the
// sample data.
Random Rand_Num = new Random();
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(Double)));
// Populate the table with sample values.
for (int i=0; i<=8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * i;
dt.Rows.Add(dr);
}
// To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
DataView dv = new DataView(dt);
return dv;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ItemsGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ItemsGrid_PageIndexChanged);
this.ItemsGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.ItemsGrid_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ItemsGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Retrieve the data source from session state.
DataTable dt = (DataTable)Session["Source"];
// Create a DataView from the DataTable.
DataView dv = new DataView(dt);
// The DataView provides an easy way to sort. Simply set the
// Sort property with the name of the field to sort by.
if(Session["sort"] == "ASC")
{
dv.Sort = e.SortExpression + " ASC";
Session["sort"] = "DESC";
}
else
{
dv.Sort = e.SortExpression + " DESC";
Session["sort"] = "ASC";
}
// Re-bind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
ItemsGrid.DataSource = dv;
ItemsGrid.DataBind();
}
private void ItemsGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
// For the DataGrid control to navigate to the correct page when
// paging is allowed, the CurrentPageIndex property must be updated
// programmatically. This process is usually accomplished in the
// event-handling method for the PageIndexChanged event.
// Set CurrentPageIndex to the page the user clicked.
ItemsGrid.CurrentPageIndex = e.NewPageIndex;
// Rebind the data to refresh the DataGrid control.
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="ItemsGrid" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 208px"
runat="server" allowsorting="True" allowpaging="True" pagesize="2"></asp:datagrid>
</form>
</body>
</html>
i.e.
If I have:
IntegerValue StringValue CurrencyValue
0 Item 0 0
1 Item 1 1.23
< >
then sort I will get:
IntegerValue StringValue CurrencyValue
8 Item 8 9.84
7 Item 7 8.61
< >
and what I want is:
IntegerValue StringValue CurrencyValue
1 Item 1 1.23
0 Item 0 0
< >
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
private void Page_Load(object sender, System.EventArgs e)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
ICollection CreateDataSource()
{
// Create a Random object to mix up the order of items in the
// sample data.
Random Rand_Num = new Random();
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(Double)));
// Populate the table with sample values.
for (int i=0; i<=8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * i;
dt.Rows.Add(dr);
}
// To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
DataView dv = new DataView(dt);
return dv;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ItemsGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ItemsGrid_PageIndexChanged);
this.ItemsGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.ItemsGrid_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ItemsGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Retrieve the data source from session state.
DataTable dt = (DataTable)Session["Source"];
// Create a DataView from the DataTable.
DataView dv = new DataView(dt);
// The DataView provides an easy way to sort. Simply set the
// Sort property with the name of the field to sort by.
if(Session["sort"] == "ASC")
{
dv.Sort = e.SortExpression + " ASC";
Session["sort"] = "DESC";
}
else
{
dv.Sort = e.SortExpression + " DESC";
Session["sort"] = "ASC";
}
// Re-bind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
ItemsGrid.DataSource = dv;
ItemsGrid.DataBind();
}
private void ItemsGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
// For the DataGrid control to navigate to the correct page when
// paging is allowed, the CurrentPageIndex property must be updated
// programmatically. This process is usually accomplished in the
// event-handling method for the PageIndexChanged event.
// Set CurrentPageIndex to the page the user clicked.
ItemsGrid.CurrentPageIndex = e.NewPageIndex;
// Rebind the data to refresh the DataGrid control.
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="ItemsGrid" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 208px"
runat="server" allowsorting="True" allowpaging="True" pagesize="2"></asp:datagrid>
</form>
</body>
</html>