Sort Command

T

Tiz

Does anybody know why a Sort Command is not being called.
I've declared the "allow sorting" in the DataGrid and
Added the delegate to handle the event. I generate the DataColumns
dinamically. Creating
new BoundColumn and adding to the DataGrida Columns
Collection. The column autogenerate is off...
The Page and Data Grid have the viewstate set to true, and every column has
ha sort expression set, but when i try to sort the column, the sort command
is not being called, i rally appreciate your help.

Thanks
 
E

Elefterios Melissaratos

Tiz said:
Does anybody know why a Sort Command is not being called.
I've declared the "allow sorting" in the DataGrid and
Added the delegate to handle the event. I generate the DataColumns
dinamically. Creating
new BoundColumn and adding to the DataGrida Columns
Collection. The column autogenerate is off...
The Page and Data Grid have the viewstate set to true, and every column has
ha sort expression set, but when i try to sort the column, the sort command
is not being called, i rally appreciate your help.

Thanks


I include the complete .aspx file and the codebehind file
that hopefully answers your question.

------- CODE BEHIND file-------------------------------------------
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 ASPNETDataGrid
{
/// <summary>
/// Summary description for Sorting.
/// </summary>
public class Sorting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DG;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
PopulateDataGrid();
}

#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.DG.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DG_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
protected DataTable CreateData()
{
DataTable dt = new DataTable();

DataColumn dc = new
DataColumn("ObjectName",System.Type.GetType("System.String"));
dt.Columns.Add(dc);

dc = new DataColumn("ID",System.Type.GetType("System.String"));
dt.Columns.Add(dc);

DataRow dr = dt.NewRow();
dr["ObjectName"] = "Automobile";
dr["ID"] = "2";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["ObjectName"] = "Boat";
dr["ID"] = "1";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["ObjectName"] = "Airplane";
dr["ID"] = "3";
dt.Rows.Add(dr);

return dt;

}
private void PopulateDataGrid()
{
BoundColumn bc = new BoundColumn();
bc.DataField = "ObjectName";
bc.HeaderText = "Item Name";
bc.SortExpression = "ObjectName";
DG.Columns.Add(bc);

bc = new BoundColumn();
bc.DataField = "ID";
bc.HeaderText = "Item ID";
bc.SortExpression = "ID";
DG.Columns.Add(bc);

DG.DataSource = CreateData();
DG.DataBind();
}

private void DG_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataTable dt = (DG.DataSource as DataTable);
if (dt != null)
{
DataView dv = dt.DefaultView;
dv.Sort = e.SortExpression;
DG.DataSource = dv;
DG.DataBind();
}

}
}
}

------------------.ASPX File --------------------------------------


<%@ Page language="c#" Codebehind="Sorting.aspx.cs"
AutoEventWireup="false" Inherits="ASPNETDataGrid.Sorting" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Sorting</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="DG" style="Z-INDEX: 101; LEFT: 76px; POSITION:
absolute; TOP: 107px" runat="server"
AllowSorting="True" AutoGenerateColumns="False"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 328px; POSITION:
absolute; TOP: 59px" runat="server"
Width="159px"></asp:Label>
</form>
</body>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top