adamshao
November 26th, 2002, 02:10 PM
I add a column programatically, and need to enable sorting. If I click the sorting head, the datagrid will disapper. However, if I hard code the column adding in the ASPX page, the datagrid shows correctly. Any suggestion to solve this problem?
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %>
<HTML>
<body>
<form id="Form1" runat="server">
<h3>DataGrid Sorting Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3" AllowSorting="true" OnSortCommand="Sort_Grid" HeaderStyle-BackColor="#00aaaa" AutoGenerateColumns="false">
</asp:DataGrid>
</form>
</body>
</HTML>
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 WebApplication10
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
string SortExpression;
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
Random Rand_Num = new Random();
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 15; i >0; i--)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * Rand_Num.Next(1, 15);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
dv.Sort=SortExpression;
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
if (SortExpression == "")
SortExpression = "IntegerValue";
ItemsGrid.DataSource = CreateDataSource();
InitializeBoundColumns();
ItemsGrid.DataBind();
}
}
private void InitializeBoundColumns()
{
ItemsGrid.AutoGenerateColumns = false;
ItemsGrid.AllowSorting = true;
HyperLinkColumn urlCol = new HyperLinkColumn();
urlCol.HeaderText="IntegerValue";
urlCol.DataNavigateUrlField="IntegerValue";
urlCol.DataNavigateUrlFormatString="detailspage.aspx?id={0}";
urlCol.DataTextField="IntegerValue";
urlCol.DataTextFormatString="{0:c}";
urlCol.SortExpression = "IntegerValue";
urlCol.Target="_new";
ItemsGrid.Columns.Add(urlCol);
}
public void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
SortExpression = e.SortExpression.ToString();
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
#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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %>
<HTML>
<body>
<form id="Form1" runat="server">
<h3>DataGrid Sorting Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3" AllowSorting="true" OnSortCommand="Sort_Grid" HeaderStyle-BackColor="#00aaaa" AutoGenerateColumns="false">
</asp:DataGrid>
</form>
</body>
</HTML>
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 WebApplication10
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
string SortExpression;
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
Random Rand_Num = new Random();
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 15; i >0; i--)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * Rand_Num.Next(1, 15);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
dv.Sort=SortExpression;
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
if (SortExpression == "")
SortExpression = "IntegerValue";
ItemsGrid.DataSource = CreateDataSource();
InitializeBoundColumns();
ItemsGrid.DataBind();
}
}
private void InitializeBoundColumns()
{
ItemsGrid.AutoGenerateColumns = false;
ItemsGrid.AllowSorting = true;
HyperLinkColumn urlCol = new HyperLinkColumn();
urlCol.HeaderText="IntegerValue";
urlCol.DataNavigateUrlField="IntegerValue";
urlCol.DataNavigateUrlFormatString="detailspage.aspx?id={0}";
urlCol.DataTextField="IntegerValue";
urlCol.DataTextFormatString="{0:c}";
urlCol.SortExpression = "IntegerValue";
urlCol.Target="_new";
ItemsGrid.Columns.Add(urlCol);
}
public void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
SortExpression = e.SortExpression.ToString();
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
#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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}