473,386 Members | 1,602 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,386 software developers and data experts.

datagrid sorting

I have just written my first page with a datagrid that allows ASC and
DESC sorting. But I want to know how I can get it so that the header
shows the type of sorting that has just been done on it (i.e. Column1
DESC). Also I would need to be able to remove this from the last column
that was sorted on so that it now says just Column2.

<%@ Page Language="C#" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

private void Page_Load()
{
if (!(Page.IsPostBack))
{
ViewState["LastSortOrder"]="DESC";
ViewState["LastSortColumn"]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Convert.ToString(Session["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
}
}

//cache can't be used as is Application level
//this code keeps data up to date on Session level (user may enter
different ODN etc)
void BindData(string strCallLog)
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

DataTable objDataTable = new DataTable();
objDataAdapter.Fill(objDataSet, "tblCallLog");

Session["LogData"] = objDataSet.Tables["tblCallLog"].DefaultView;
dgLog.DataSource = Session["LogData"];
dgLog.DataBind();
}

void dgLog_Paged(object sender, DataGridPageChangedEventArgs e)
{
dgLog.CurrentPageIndex = e.NewPageIndex;
BindData(Convert.ToString(Session["CallLog"]));
}

void dgLog_Sort(object sender, DataGridSortCommandEventArgs e)
{
string newSortColumn= e.SortExpression.ToString();
string newSortOrder="ASC"; // default
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
if (newSortColumn.Equals(lastSortColumn) &&
lastSortOrder.Equals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder="ASC";}
ViewState["LastSortOrder"]= newSortOrder;
ViewState["LastSortColumn"]= newSortColumn;
dgLog.CurrentPageIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpression + " " + newSortOrder;

BindData(Convert.ToString(Session["CallLog"]));
}
</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form ID="Log" runat="server">
<div style="vertical-align
top;height:400px;width:570px;overflow:scroll;">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#00015E"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-Font-Bold="true"
BorderColor="black"
BackColor="#0099CC"
forecolor="#FFFFFF"
AllowPaging="true"
PageSize="10"
OnPageIndexChanged="dgLog_Paged"
AutoGenerateColumns="False"
AllowSorting="true"
OnSortCommand="dgLog_Sort">

<Columns>
<asp:BoundColumn DataField="LogNum" HeaderText="LogNum"
SortExpression="LogNum" />
<asp:BoundColumn DataField="InceptTime"
HeaderText="InceptTime" SortExpression="InceptTime" />
<asp:BoundColumn DataField="InSpan" HeaderText="InSpan" />
<asp:BoundColumn DataField="InChan" HeaderText="InChan"
SortExpression="InChan" />
<asp:BoundColumn DataField="OutSpan" HeaderText="OutSpan"
SortExpression="OutSpan" />
<asp:BoundColumn DataField="OutChan" HeaderText="OutChan" />
</Columns>

<PagerStyle
Mode="NumericPages"
HorizontalAlign="Left"
ForeColor="#00015E"
BackColor="#dcdcdc"
NextPageText="Next"
PrevPageText="Previous" />

</asp:DataGrid>
</div>
</form>
</body>
</html>
Any help would be really appreciated.

Cheers,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #1
2 1499
Mike,

DataGrid1.Columns(0).HeaderText = "Column 1 Desc"

Where: Columns([zero based column index])

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have just written my first page with a datagrid that allows ASC and
DESC sorting. But I want to know how I can get it so that the header
shows the type of sorting that has just been done on it (i.e. Column1
DESC). Also I would need to be able to remove this from the last column
that was sorted on so that it now says just Column2.

<%@ Page Language="C#" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

private void Page_Load()
{
if (!(Page.IsPostBack))
{
ViewState["LastSortOrder"]="DESC";
ViewState["LastSortColumn"]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Convert.ToString(Session["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
}
}

//cache can't be used as is Application level
//this code keeps data up to date on Session level (user may enter
different ODN etc)
void BindData(string strCallLog)
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

DataTable objDataTable = new DataTable();
objDataAdapter.Fill(objDataSet, "tblCallLog");

Session["LogData"] = objDataSet.Tables["tblCallLog"].DefaultView;
dgLog.DataSource = Session["LogData"];
dgLog.DataBind();
}

void dgLog_Paged(object sender, DataGridPageChangedEventArgs e)
{
dgLog.CurrentPageIndex = e.NewPageIndex;
BindData(Convert.ToString(Session["CallLog"]));
}

void dgLog_Sort(object sender, DataGridSortCommandEventArgs e)
{
string newSortColumn= e.SortExpression.ToString();
string newSortOrder="ASC"; // default
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
if (newSortColumn.Equals(lastSortColumn) &&
lastSortOrder.Equals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder="ASC";}
ViewState["LastSortOrder"]= newSortOrder;
ViewState["LastSortColumn"]= newSortColumn;
dgLog.CurrentPageIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpression + " " + newSortOrder;

BindData(Convert.ToString(Session["CallLog"]));
}
</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form ID="Log" runat="server">
<div style="vertical-align
top;height:400px;width:570px;overflow:scroll;">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#00015E"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-Font-Bold="true"
BorderColor="black"
BackColor="#0099CC"
forecolor="#FFFFFF"
AllowPaging="true"
PageSize="10"
OnPageIndexChanged="dgLog_Paged"
AutoGenerateColumns="False"
AllowSorting="true"
OnSortCommand="dgLog_Sort">

<Columns>
<asp:BoundColumn DataField="LogNum" HeaderText="LogNum"
SortExpression="LogNum" />
<asp:BoundColumn DataField="InceptTime"
HeaderText="InceptTime" SortExpression="InceptTime" />
<asp:BoundColumn DataField="InSpan" HeaderText="InSpan" />
<asp:BoundColumn DataField="InChan" HeaderText="InChan"
SortExpression="InChan" />
<asp:BoundColumn DataField="OutSpan" HeaderText="OutSpan"
SortExpression="OutSpan" />
<asp:BoundColumn DataField="OutChan" HeaderText="OutChan" />
</Columns>

<PagerStyle
Mode="NumericPages"
HorizontalAlign="Left"
ForeColor="#00015E"
BackColor="#dcdcdc"
NextPageText="Next"
PrevPageText="Previous" />

</asp:DataGrid>
</div>
</form>
</body>
</html>
Any help would be really appreciated.

Cheers,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #2
Thanks for your help Justin. I decided not to do it a slightly
different way as it's what my users would prefer, but it's really useful
to know there is another way to solve this problem as well.

Cheers,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #3

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

Similar topics

0
by: Chris Mayers | last post by:
I have a Windows Forms DataGrid that has a DataView as a datasource. My problem is that I want the datagrid to exhibit some special sorting properties when the header rows are clicked on. From...
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...
1
by: Sargas Atum | last post by:
Hi all, 1. I have a problem with cell selection in a table in a DataGrid. I dont want that anybody writes in the cells. That was not a problem I changed them to "read only", but if I am going...
3
by: melanieab | last post by:
Hi, I'm programatically sorting in a datagrid. When a column header is clicked, the sort happens twice for some reason, making it looks like it only sorts in descending order. I can tell it...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
1
by: Jeremy | last post by:
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...
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...
5
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but...
1
by: ECD | last post by:
Hello all, I can usually find solutions to my .NET problems by searching these groups, but I'm stumped on this one. I have a datagrid in VB.NET (2.0 framework). I want to disable sorting on...
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.