Sort order for Gridview?

G

Guest

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
 
S

sloan

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(((MyObjectToCompare )y).Age );

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


}
 
G

Guest

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
 
G

Guest

Sloan,

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

regards,
Bill
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top