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

Sort order for Gridview?

Hi,

I have a gridview that I am displaying from an Eventlog. Is there any way to
sort this with the newest entries at the top rather than the way it is
delivered from the collection? I don't want to enable sorting to let the user
sort. Here is my code to bind:

System.Diagnostics.EventLog log = new
System.Diagnostics.EventLog("MyEventLog");
System.Diagnostics.EventLogEntryCollection entries = log.Entries;
BZPEventLog.DataSource = entries;
BZPEventLog.DataBind();

thanks in advance!
Bill

Mar 29 '06 #1
3 2213

You have to get its innerList... and .Sort in the inner list.

You also have to create a custom IComparer.

Here is a sample to get you started:

This assumes you have a
MyObjectToCompare object (which you will change to whatever a single object
of your collection is)

and

MyObjectToCompare.Age
MyObjectToCompare.Name

you'll replace with the properties you want to sort on.


using System;
using System.Collections;
namespace MyApp
{
internal sealed class MyFirstComparer : IComparer
{
private MyColumns m_sortValue = MyColumns.None ;

public enum MyColumns
{
None = 0 , Age = 1 , Name = 2
}

public MyFirstComparer(MyColumns sortValue)
{
m_sortValue = sortValue;
}

public int Compare(object x,object y)
{
switch(m_sortValue)
{

case MyColumns.None :
return 0;
case MyColumns.Name :

return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
//break;
case MyColumns.Age :
return
(MyObjectToCompare)x).Age.CompareTo(((MyObjectToCo mpare )y).Age );

default:
return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
// break;
}
}
}
}


"Bill44077" <Bi*******@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
Hi,

I have a gridview that I am displaying from an Eventlog. Is there any way to sort this with the newest entries at the top rather than the way it is
delivered from the collection? I don't want to enable sorting to let the user sort. Here is my code to bind:

System.Diagnostics.EventLog log = new
System.Diagnostics.EventLog("MyEventLog");
System.Diagnostics.EventLogEntryCollection entries = log.Entries; BZPEventLog.DataSource = entries;
BZPEventLog.DataBind();

thanks in advance!
Bill

Mar 29 '06 #2
Sloan,

Thanks for the quick reply.

E-Gads comes to mind :) I was hoping for something simple.

I'll give it a go...

regards,
Bill
"sloan" wrote:

You have to get its innerList... and .Sort in the inner list.

You also have to create a custom IComparer.

Here is a sample to get you started:

This assumes you have a
MyObjectToCompare object (which you will change to whatever a single object
of your collection is)

and

MyObjectToCompare.Age
MyObjectToCompare.Name

you'll replace with the properties you want to sort on.


using System;
using System.Collections;
namespace MyApp
{
internal sealed class MyFirstComparer : IComparer
{
private MyColumns m_sortValue = MyColumns.None ;

public enum MyColumns
{
None = 0 , Age = 1 , Name = 2
}

public MyFirstComparer(MyColumns sortValue)
{
m_sortValue = sortValue;
}

public int Compare(object x,object y)
{
switch(m_sortValue)
{

case MyColumns.None :
return 0;
case MyColumns.Name :

return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
//break;
case MyColumns.Age :
return
(MyObjectToCompare)x).Age.CompareTo(((MyObjectToCo mpare )y).Age );

default:
return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
// break;
}
}
}
}


"Bill44077" <Bi*******@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
Hi,

I have a gridview that I am displaying from an Eventlog. Is there any way

to
sort this with the newest entries at the top rather than the way it is
delivered from the collection? I don't want to enable sorting to let the

user
sort. Here is my code to bind:

System.Diagnostics.EventLog log = new
System.Diagnostics.EventLog("MyEventLog");
System.Diagnostics.EventLogEntryCollection entries =

log.Entries;
BZPEventLog.DataSource = entries;
BZPEventLog.DataBind();

thanks in advance!
Bill


Mar 29 '06 #3
Sloan,

Thanks for the quick reply - go figure, I was hoping for something really
simple like a propoerty setting or something.

regards,
Bill
"sloan" wrote:

You have to get its innerList... and .Sort in the inner list.

You also have to create a custom IComparer.

Here is a sample to get you started:

This assumes you have a
MyObjectToCompare object (which you will change to whatever a single object
of your collection is)

and

MyObjectToCompare.Age
MyObjectToCompare.Name

you'll replace with the properties you want to sort on.


using System;
using System.Collections;
namespace MyApp
{
internal sealed class MyFirstComparer : IComparer
{
private MyColumns m_sortValue = MyColumns.None ;

public enum MyColumns
{
None = 0 , Age = 1 , Name = 2
}

public MyFirstComparer(MyColumns sortValue)
{
m_sortValue = sortValue;
}

public int Compare(object x,object y)
{
switch(m_sortValue)
{

case MyColumns.None :
return 0;
case MyColumns.Name :

return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
//break;
case MyColumns.Age :
return
(MyObjectToCompare)x).Age.CompareTo(((MyObjectToCo mpare )y).Age );

default:
return ((MyObjectToCompare)x).Name
..CompareTo(((MyObjectToCompare )y).Name );
// break;
}
}
}
}


"Bill44077" <Bi*******@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
Hi,

I have a gridview that I am displaying from an Eventlog. Is there any way

to
sort this with the newest entries at the top rather than the way it is
delivered from the collection? I don't want to enable sorting to let the

user
sort. Here is my code to bind:

System.Diagnostics.EventLog log = new
System.Diagnostics.EventLog("MyEventLog");
System.Diagnostics.EventLogEntryCollection entries =

log.Entries;
BZPEventLog.DataSource = entries;
BZPEventLog.DataBind();

thanks in advance!
Bill


Mar 29 '06 #4

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

Similar topics

2
by: Daves | last post by:
I have a gridview displaying News items and fetches the data through a (little complicated) sql stored procedure... The sorting seems to be done in the gridview since there is no sorting...
1
by: Smokey Grindle | last post by:
does anyone know of a good example of asp.net 2.0's gridview showing a sort order arrow in the column header correctly and working? I cant seem to get what ive found on google to work (see previous...
1
by: needin4mation | last post by:
Hi, I have a select that uses union. The gridview populates fine with the two tables' returned data. But now I want to sort the resulting dataset as if it were one set of data, since I know of no...
0
by: Ryan H | last post by:
Hi, If I call GridView.Sort("MyColumn1, MyColumn2", SortDirection.Descending), I assume that the sort order will be equivalent to the SQL statement "...ORDER BY MyColumn2 DESC, MyColumn1 DESC"....
0
by: rmgalante | last post by:
Hi, I've been experimenting with the ASP.Net GridView and encountered some interesting issues that I thought I would share. I have a page that loads a GridView with a generic collection of...
4
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
For performance reasons i can not set a sort order on a dataset returned from a stored procedure. Ideally in the sp i'd set the company name as the order and just load the gridview, however the...
12
by: Cindy Lee | last post by:
When I do a sorta on 1 table, then the other table goes back to the original order. What can I set so, it keeps the order of the other current gridview's order. I set all the gridview values...
2
by: Ryan H | last post by:
Given 2 records with 1 'Name' column (varchar): "Tri-Star" "Triangle" GridView.Sort("Name", SortDirection.Ascending) will sort the records like so: 1. "Triangle" 2. "Tri-Star" While a...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.