DataGrid and Simple Sorting

G

Guest

Hi.

I am having a problem doing a simple sorting. Would someone be cool enough
to take a look and advise? It doesn't sort.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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;
using System.Configuration;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

namespace Wells.Compliance.UI.SAR
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected ComponentArt.Web.UI.Menu Menu1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;

private string m_sSortColumn = "";
private string m_sSortOrder = "";

private void Page_Load(object sender, System.EventArgs e)
{
//if (!IsPostBack)
//{
//if (m_sSortColumn == "")
//{
Session["SortColumn"] = "InsertDate";
Session["SortOrder"] = " asc";
//}

BindGrid();
//DataGrid1.DataSource = GetDataSource();
//DataGrid1.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.DataGrid1.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortData);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

}

private ICollection GetDataSource()
{
string sConn = ConfigurationSettings.AppSettings["ConnectString"];
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("PackageID", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Columns.Add(new DataColumn("InsertDate", typeof(string)));

SqlDataReader sdr = SqlHelper.ExecuteReader(sConn, "spGetUnusualPackage");

while (sdr.Read())
{
dr = dt.NewRow();

dr[0] = sdr["PackageID"].ToString();
dr[1] = sdr["Status"].ToString();
dr[2] = sdr["InsertDate"].ToString();

dt.Rows.Add(dr);
}

// To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
//Session["SortOrder"] = " asc";

DataView dv = new DataView(dt);
dv.Sort = Session["SortColumn"].ToString() +
Session["SortOrder"].ToString();

return dv;
}

private void SortData(Object sender, DataGridSortCommandEventArgs e)
{
Session["SortColumn"] = (string) e.SortExpression;

if (Session["SortOrder"].ToString() == " desc")
Session["SortOrder"] = " asc";
else
Session["SortOrder"] = " desc";

BindGrid();

// 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.

//dv.Sort = e.SortExpression; //+ m_sSortOrder;

// Re-bind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
//DataGrid1.DataSource = dv;
//DataGrid1.DataBind();
}

private void BindGrid()
{
DataGrid1.DataSource = GetDataSource();
DataGrid1.DataBind();
}
}
}
 
G

Guest

I don't know if this would help, but whenever I put the cursor on the column
haeder, it displays the following message:

javascript: __doPostBack(DataGrid1$_ctl14_ctl0', '')

Any help is appreciated.

thejackofall said:
Hi.

I am having a problem doing a simple sorting. Would someone be cool enough
to take a look and advise? It doesn't sort.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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;
using System.Configuration;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

namespace Wells.Compliance.UI.SAR
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected ComponentArt.Web.UI.Menu Menu1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;

private string m_sSortColumn = "";
private string m_sSortOrder = "";

private void Page_Load(object sender, System.EventArgs e)
{
//if (!IsPostBack)
//{
//if (m_sSortColumn == "")
//{
Session["SortColumn"] = "InsertDate";
Session["SortOrder"] = " asc";
//}

BindGrid();
//DataGrid1.DataSource = GetDataSource();
//DataGrid1.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.DataGrid1.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortData);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

}

private ICollection GetDataSource()
{
string sConn = ConfigurationSettings.AppSettings["ConnectString"];
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("PackageID", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Columns.Add(new DataColumn("InsertDate", typeof(string)));

SqlDataReader sdr = SqlHelper.ExecuteReader(sConn, "spGetUnusualPackage");

while (sdr.Read())
{
dr = dt.NewRow();

dr[0] = sdr["PackageID"].ToString();
dr[1] = sdr["Status"].ToString();
dr[2] = sdr["InsertDate"].ToString();

dt.Rows.Add(dr);
}

// To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
//Session["SortOrder"] = " asc";

DataView dv = new DataView(dt);
dv.Sort = Session["SortColumn"].ToString() +
Session["SortOrder"].ToString();

return dv;
}

private void SortData(Object sender, DataGridSortCommandEventArgs e)
{
Session["SortColumn"] = (string) e.SortExpression;

if (Session["SortOrder"].ToString() == " desc")
Session["SortOrder"] = " asc";
else
Session["SortOrder"] = " desc";

BindGrid();

// 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.

//dv.Sort = e.SortExpression; //+ m_sSortOrder;

// Re-bind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
//DataGrid1.DataSource = dv;
//DataGrid1.DataBind();
}

private void BindGrid()
{
DataGrid1.DataSource = GetDataSource();
DataGrid1.DataBind();
}
}
}
 

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

Similar Threads


Top