I have dataGrid that I am filling from a List Collection and need to sort it
by the various columns.
So I need to be able to sort the Collection and found that you have to set
up your own sorting functions to make it work. I have build the following 3
sorting
functions for the 3 columns and have to call each one specifically.
I am trying to find a way to cut simplify the functions and calls to make
this a more
general class. At the moment, if I add another column I need to add another
sorting method
and another call from my switch.
Here is the Class and Sorts
*************** *************** *************** *
using System;
using System.Collecti ons.Generic;
namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<Dis playAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValu e = 0;
string mstrTransaction Description = "";
public string DisplayName
{
get{ return mstrDisplayName ; }
set{ mstrDisplayName = value;}
}
public double DisplayValue
{
get{ return mdblDisplayValu e;}
set{ mdblDisplayValu e = value;}
}
public string TransactionDesc ription
{
get{ return mstrTransaction Description;}
set{ mstrTransaction Description = value;}
}
// Sorts
public static int CompareName(Dis playAmount c1, DisplayAmount c2)
{
return c1.DisplayName. CompareTo(c2.Di splayName);
}
public static int CompareValue(Di splayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue .CompareTo(c2.D isplayValue);
}
public static int CompareTransact ionDescription( DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionD escription.Comp areTo(c2.Transa ctionDescriptio n);
}
}
// Define Collections here
public class DisplayAmountCo llection : List<DisplayAmo unt{ }
}
*************** *************** *************** ***************
To sort the collection I call the following function with the Collection,
the name of the column to sort and
the direction.
*************** *************** *************** *************** *
protected void SortList(ref DisplayAmountCo llection ocol, string
sortExpression, string sortDirection)
{
Comparison<Disp layAmountoColDe l = null;
switch (sortExpression )
{
case "DisplayNam e":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reName);
ocol.Sort(oColD el);
break;
case "DisplayVal ue":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reValue);
ocol.Sort(oColD el);
break;
case "TransactionDes cription":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reTransactionDe scription);
ocol.Sort(oColD el);
break;
}
if (sortDirection == "DESC") ocol.Reverse();
}
*************** *************** *************** *************** *
I'd like to see if I can simplify this if possible. If I have 10 columns, I
would have 10 case statements.
Also, I was trying to do a similar thing with my "Comparison<Dis playAmount>"
as I do with my "DisplayListCol lection".
I don't have to do:
List<DisplayAmo untd1 = null;
in my code. I put this in my class:
public class DisplayAmountCo llection : List<DisplayAmo unt{ }
and then call it in my program as:
DisplayAmountCo llection dc = null;
But I can't seem to the same with my Comparison<tsta tement without getting
an error. If I do the following:
public class DisplayAmountSo rt : Comparison<Disp layAmount{ }
I get the error:
cannot derive from sealed type 'System.Compari son<DisplayAmou nt>'
Why can I do it with List but not with Comparison?
Thanks,
Tom 3 4142
Hi,
If you are always going to sort by using only one column you can use a code
I created a time ago just for this: http://groups.google.com/group/micro...97990b9dcd65e2
The good part about it is that the "sorted" class do not have knowledge of
it. you can sort a collection of ANY type.
You might ahve to convert it to use IComparer<Tas the code was created in
1.1
--
Ignacio Machin http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"tshad" <tf*@dslextreme .comwrote in message
news:uY******** ******@TK2MSFTN GP04.phx.gbl...
>I have dataGrid that I am filling from a List Collection and need to sort it
by the various columns.
So I need to be able to sort the Collection and found that you have to set
up your own sorting functions to make it work. I have build the following
3 sorting
functions for the 3 columns and have to call each one specifically.
I am trying to find a way to cut simplify the functions and calls to make
this a more
general class. At the moment, if I add another column I need to add
another sorting method
and another call from my switch.
Here is the Class and Sorts
*************** *************** *************** *
using System;
using System.Collecti ons.Generic;
namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<Dis playAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValu e = 0;
string mstrTransaction Description = "";
public string DisplayName
{
get{ return mstrDisplayName ; }
set{ mstrDisplayName = value;}
}
public double DisplayValue
{
get{ return mdblDisplayValu e;}
set{ mdblDisplayValu e = value;}
}
public string TransactionDesc ription
{
get{ return mstrTransaction Description;}
set{ mstrTransaction Description = value;}
}
// Sorts
public static int CompareName(Dis playAmount c1, DisplayAmount c2)
{
return c1.DisplayName. CompareTo(c2.Di splayName);
}
public static int CompareValue(Di splayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue .CompareTo(c2.D isplayValue);
}
public static int CompareTransact ionDescription( DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionD escription.Comp areTo(c2.Transa ctionDescriptio n);
}
}
// Define Collections here
public class DisplayAmountCo llection : List<DisplayAmo unt{ }
}
*************** *************** *************** ***************
To sort the collection I call the following function with the Collection,
the name of the column to sort and
the direction.
*************** *************** *************** *************** *
protected void SortList(ref DisplayAmountCo llection ocol, string
sortExpression, string sortDirection)
{
Comparison<Disp layAmountoColDe l = null;
switch (sortExpression )
{
case "DisplayNam e":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reName);
ocol.Sort(oColD el);
break;
case "DisplayVal ue":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reValue);
ocol.Sort(oColD el);
break;
case "TransactionDes cription":
oColDel = new
Comparison<Disp layAmount>(Disp layAmount.Compa reTransactionDe scription);
ocol.Sort(oColD el);
break;
}
if (sortDirection == "DESC") ocol.Reverse();
}
*************** *************** *************** *************** *
I'd like to see if I can simplify this if possible. If I have 10 columns,
I would have 10 case statements.
Also, I was trying to do a similar thing with my
"Comparison<Dis playAmount>" as I do with my "DisplayListCol lection".
I don't have to do:
List<DisplayAmo untd1 = null;
in my code. I put this in my class:
public class DisplayAmountCo llection : List<DisplayAmo unt{ }
and then call it in my program as:
DisplayAmountCo llection dc = null;
But I can't seem to the same with my Comparison<tsta tement without
getting an error. If I do the following:
public class DisplayAmountSo rt : Comparison<Disp layAmount{ }
I get the error:
cannot derive from sealed type 'System.Compari son<DisplayAmou nt>'
Why can I do it with List but not with Comparison?
Thanks,
Tom
But if I am passing it with "ref", doesn't that say to pass it by reference
and not value?
This gets tricky to explain... the short version is that List<T(etc)
is a reference-type, which means that even without "ref" all you are
passing is the address of the list (on the managed heap). If you use
"ref" here, then instead of passing the address of the list, it passes
the address of the *variable* in the calling code (typically on the
stack, although it could be a field etc). The only things "ref" gives
you here is the ability to update the variable directly and have that
reassignment propegate to the calling method - but you don't need to
do that. Neither way (alone) causes the list to clone itself.
Jon Skeet puts it better: http://www.pobox.com/~skeet/csharp/parameters.html
Marc
Which cited links? http://groups.google.com/group/micro...23312d4c9ae353
(this is for a BindingList<Tim plementation of IBindingList-based and
IBindingListVie w-based sorting, which is *exactly* what is used
automatically when you click on the column headers).
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Elliott |
last post by:
I am looking to use a field attribute in a to find and use a property. If given
Find("Record Artist", "Billy Joel");
I am looking to find the first instance in the collection where the value
the property -=> Artist <=-( because of the Attribute "Record Artist")
equals "Billy Joel".
I didn't know if this is possible.
Please keep in mind that I know I need to take care of data types and
error detection/correction. I am trying to...
|
by: Eric Lilja |
last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
|
by: Robert Zurer |
last post by:
Assuming that I have created a strongly typed collection and overridden the
appropriate methods, i.e. this, Add, Insert etc., so that a sort order is
maintained, it's still very possible for a property of the
'ListMemberObject' which is instrumental in the sort to be modified
unbeknownst to the list thereby defeating the sort.
One way to keep the list sorted is to add a 'ParentList' property to the
ListMemberObject and notify that...
|
by: William Stacey [MVP] |
last post by:
int list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
--
William Stacey, MVP
|
by: Learner |
last post by:
Hello,
Here is the code snippet I got strucked at.
I am unable to convert the below line of code to its equavalent vb.net
code. could some one please help me with this?
static public List<RoleData> GetRoles()
{
return GetRoles(null, false);
}
| |
by: bonk |
last post by:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.
|
by: David Longnecker |
last post by:
I'm working to create a base framework for our organization for web and client-side
applications. The framework interfaces with several of our systems and provides
the business and data layer connectivity for basic operations and such.
I've ran into a snag that I just can't think myself out of.
Here's an example:
I have an object for a Student called StudentRecord. It has properties such
as name, grade, identification number, etc.
|
by: tshad |
last post by:
I have the following code:
***************************************
public class Test
{
....
public static TestCollection MyNewCollection()
{
}
}
|
by: =?Utf-8?B?anAybXNmdA==?= |
last post by:
I'm pulling records from or database by dates, and there are multiple records
for each part number.
The part numbers are being stored as strings in a List in a custom Employee
class:
List<stringParts = new List<string>();
If a part number is not already in the list, a new entry is created for an
employee that gets credited this part number:
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |