473,785 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="te xt/html"
ResponseEncodin g="iso-8859-1" %>
<%@ Import Namespace="Syst em" %>
<%@ Import Namespace="Syst em.Configuratio n" %>
<%@ Import Namespace="Syst em.Web.UI" %>
<%@ Import Namespace="Syst em.Web.UI.WebCo ntrols" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>

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

private void Page_Load()
{
if (!(Page.IsPostB ack))
{
ViewState["LastSortOr der"]="DESC";
ViewState["LastSortColumn "]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Conver t.ToString(Sess ion["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewSta te["LastSortColumn "];
string lastSortOrder= (string)ViewSta te["LastSortOr der"];
}
}

//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(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter( strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

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

Session["LogData"] = objDataSet.Tabl es["tblCallLog "].DefaultView;
dgLog.DataSourc e = Session["LogData"];
dgLog.DataBind( );
}

void dgLog_Paged(obj ect sender, DataGridPageCha ngedEventArgs e)
{
dgLog.CurrentPa geIndex = e.NewPageIndex;
BindData(Conver t.ToString(Sess ion["CallLog"]));
}

void dgLog_Sort(obje ct sender, DataGridSortCom mandEventArgs e)
{
string newSortColumn= e.SortExpressio n.ToString();
string newSortOrder="A SC"; // default
string lastSortColumn= (string)ViewSta te["LastSortColumn "];
string lastSortOrder= (string)ViewSta te["LastSortOr der"];
if (newSortColumn. Equals(lastSort Column) &&
lastSortOrder.E quals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder=" ASC";}
ViewState["LastSortOr der"]= newSortOrder;
ViewState["LastSortColumn "]= newSortColumn;
dgLog.CurrentPa geIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpressio n + " " + newSortOrder;

BindData(Conver t.ToString(Sess ion["CallLog"]));
}
</script>

<html>
<head>
<title>Untitl ed 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:400p x;width:570px;o verflow:scroll; ">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#000 15E"
HeaderStyle-BackColor="#dcd cdc"
HeaderStyle-Font-Bold="true"
BorderColor="bl ack"
BackColor="#009 9CC"
forecolor="#FFF FFF"
AllowPaging="tr ue"
PageSize="10"
OnPageIndexChan ged="dgLog_Page d"
AutoGenerateCol umns="False"
AllowSorting="t rue"
OnSortCommand=" dgLog_Sort">

<Columns>
<asp:BoundColum n DataField="LogN um" HeaderText="Log Num"
SortExpression= "LogNum" />
<asp:BoundColum n DataField="Ince ptTime"
HeaderText="Inc eptTime" SortExpression= "InceptTime " />
<asp:BoundColum n DataField="InSp an" HeaderText="InS pan" />
<asp:BoundColum n DataField="InCh an" HeaderText="InC han"
SortExpression= "InChan" />
<asp:BoundColum n DataField="OutS pan" HeaderText="Out Span"
SortExpression= "OutSpan" />
<asp:BoundColum n DataField="OutC han" HeaderText="Out Chan" />
</Columns>

<PagerStyle
Mode="NumericPa ges"
HorizontalAlign ="Left"
ForeColor="#000 15E"
BackColor="#dcd cdc"
NextPageText="N ext"
PrevPageText="P revious" />

</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 1514
Mike,

DataGrid1.Colum ns(0).HeaderTex t = "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*@telcoelect ronics.co.uk> wrote in message
news:%2******** ********@TK2MSF TNGP09.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="te xt/html"
ResponseEncodin g="iso-8859-1" %>
<%@ Import Namespace="Syst em" %>
<%@ Import Namespace="Syst em.Configuratio n" %>
<%@ Import Namespace="Syst em.Web.UI" %>
<%@ Import Namespace="Syst em.Web.UI.WebCo ntrols" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>

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

private void Page_Load()
{
if (!(Page.IsPostB ack))
{
ViewState["LastSortOr der"]="DESC";
ViewState["LastSortColumn "]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Conver t.ToString(Sess ion["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewSta te["LastSortColumn "];
string lastSortOrder= (string)ViewSta te["LastSortOr der"];
}
}

//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(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter( strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

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

Session["LogData"] = objDataSet.Tabl es["tblCallLog "].DefaultView;
dgLog.DataSourc e = Session["LogData"];
dgLog.DataBind( );
}

void dgLog_Paged(obj ect sender, DataGridPageCha ngedEventArgs e)
{
dgLog.CurrentPa geIndex = e.NewPageIndex;
BindData(Conver t.ToString(Sess ion["CallLog"]));
}

void dgLog_Sort(obje ct sender, DataGridSortCom mandEventArgs e)
{
string newSortColumn= e.SortExpressio n.ToString();
string newSortOrder="A SC"; // default
string lastSortColumn= (string)ViewSta te["LastSortColumn "];
string lastSortOrder= (string)ViewSta te["LastSortOr der"];
if (newSortColumn. Equals(lastSort Column) &&
lastSortOrder.E quals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder=" ASC";}
ViewState["LastSortOr der"]= newSortOrder;
ViewState["LastSortColumn "]= newSortColumn;
dgLog.CurrentPa geIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpressio n + " " + newSortOrder;

BindData(Conver t.ToString(Sess ion["CallLog"]));
}
</script>

<html>
<head>
<title>Untitl ed 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:400p x;width:570px;o verflow:scroll; ">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#000 15E"
HeaderStyle-BackColor="#dcd cdc"
HeaderStyle-Font-Bold="true"
BorderColor="bl ack"
BackColor="#009 9CC"
forecolor="#FFF FFF"
AllowPaging="tr ue"
PageSize="10"
OnPageIndexChan ged="dgLog_Page d"
AutoGenerateCol umns="False"
AllowSorting="t rue"
OnSortCommand=" dgLog_Sort">

<Columns>
<asp:BoundColum n DataField="LogN um" HeaderText="Log Num"
SortExpression= "LogNum" />
<asp:BoundColum n DataField="Ince ptTime"
HeaderText="Inc eptTime" SortExpression= "InceptTime " />
<asp:BoundColum n DataField="InSp an" HeaderText="InS pan" />
<asp:BoundColum n DataField="InCh an" HeaderText="InC han"
SortExpression= "InChan" />
<asp:BoundColum n DataField="OutS pan" HeaderText="Out Span"
SortExpression= "OutSpan" />
<asp:BoundColum n DataField="OutC han" HeaderText="Out Chan" />
</Columns>

<PagerStyle
Mode="NumericPa ges"
HorizontalAlign ="Left"
ForeColor="#000 15E"
BackColor="#dcd cdc"
NextPageText="N ext"
PrevPageText="P revious" />

</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
1645
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 reading round the newsgroups, it seems that a custom 'IComparer' is the answer, but I'm not quite sure how to do this. Really, how to get at the IComparer that the DataGrid/DataView uses for sorting. Can anyone point me towards a nice example...
2
945
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 datagrid. HTML Datagrid1 TemplateColumn Table Header information Detail Information
1
2061
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 to scroll the table up and down or from right to the left the content of the active cell will be selected as if for copy/paste. How can I deactivate such a behaviour?
3
3127
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 sorts twice because I inserted a messagebox in the dgMouse (MouseUp) event. Before ok is pressed, the table changes from the order it was loaded to ascending order. After ok is pressed, it goes to descending. The code is below. Any idea why...
7
2467
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 Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here
1
2353
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 page. i.e. If I have: IntegerValue StringValue CurrencyValue 0 Item 0 0 1 Item 1 1.23
4
2116
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 codebehind file. I am using c#. Thanks Manny
5
2534
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 I cannot sort the data when I click on the column header. I have set the tablestype.allowsorting = true, but this has no effect.
1
7142
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 the first column in the grid only. I havent found a way to reliably do this yet. I tried putting the following code in the datagrid's mouse down event Dim hti As DataGrid.HitTestInfo
0
2092
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 help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.