473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

DataGrid and Simple Sorting

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.DataGridSortCommandEvent Handler(this.SortData);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
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();
}
}
}

--
Be Cool!
Nov 22 '05 #1
1 1751
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" wrote:
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.DataGridSortCommandEvent Handler(this.SortData);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
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();
}
}
}

--
Be Cool!

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
2
by: Ken Tucker | last post by:
I've read about this issue in many articles across the net... But haven't found a solution. I see all kinds of custom code to perform sorting with datagrids, but my example is so simple, I must...
4
by: Mark Travis | last post by:
Hi all, I have written a simple Web Application that displays a query result onto a page using the ASP DataGrid. To Digress ======= Development information about the page is as follows 1....
4
by: Manny Chohan | last post by:
hi guys, my code is returning an array and i need to create datagrid so that i can have sorting and implement prev....next function on it to navigate. is there any way this can be done in...
1
by: simonyong | last post by:
hello, everyone im a newbie to asp.net, getting nut with just a simple stuff please give me some direction How to use dropdownlist when user select one of the method in dropdownlist The fields...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.