T
tshad
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.Collections.Generic;
namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<DisplayAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValue = 0;
string mstrTransactionDescription = "";
public string DisplayName
{
get{ return mstrDisplayName; }
set{ mstrDisplayName = value;}
}
public double DisplayValue
{
get{ return mdblDisplayValue;}
set{ mdblDisplayValue = value;}
}
public string TransactionDescription
{
get{ return mstrTransactionDescription;}
set{ mstrTransactionDescription = value;}
}
// Sorts
public static int CompareName(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayName.CompareTo(c2.DisplayName);
}
public static int CompareValue(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue.CompareTo(c2.DisplayValue);
}
public static int CompareTransactionDescription(DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionDescription.CompareTo(c2.TransactionDescription);
}
}
// Define Collections here
public class DisplayAmountCollection : List<DisplayAmount> { }
}
************************************************************
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 DisplayAmountCollection ocol, string
sortExpression, string sortDirection)
{
Comparison<DisplayAmount> oColDel = null;
switch (sortExpression)
{
case "DisplayName":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareName);
ocol.Sort(oColDel);
break;
case "DisplayValue":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareValue);
ocol.Sort(oColDel);
break;
case "TransactionDescription":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareTransactionDescription);
ocol.Sort(oColDel);
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<DisplayAmount>"
as I do with my "DisplayListCollection".
I don't have to do:
List<DisplayAmount> d1 = null;
in my code. I put this in my class:
public class DisplayAmountCollection : List<DisplayAmount> { }
and then call it in my program as:
DisplayAmountCollection dc = null;
But I can't seem to the same with my Comparison<t> statement without getting
an error. If I do the following:
public class DisplayAmountSort : Comparison<DisplayAmount> { }
I get the error:
cannot derive from sealed type 'System.Comparison<DisplayAmount>'
Why can I do it with List but not with Comparison?
Thanks,
Tom
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.Collections.Generic;
namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<DisplayAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValue = 0;
string mstrTransactionDescription = "";
public string DisplayName
{
get{ return mstrDisplayName; }
set{ mstrDisplayName = value;}
}
public double DisplayValue
{
get{ return mdblDisplayValue;}
set{ mdblDisplayValue = value;}
}
public string TransactionDescription
{
get{ return mstrTransactionDescription;}
set{ mstrTransactionDescription = value;}
}
// Sorts
public static int CompareName(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayName.CompareTo(c2.DisplayName);
}
public static int CompareValue(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue.CompareTo(c2.DisplayValue);
}
public static int CompareTransactionDescription(DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionDescription.CompareTo(c2.TransactionDescription);
}
}
// Define Collections here
public class DisplayAmountCollection : List<DisplayAmount> { }
}
************************************************************
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 DisplayAmountCollection ocol, string
sortExpression, string sortDirection)
{
Comparison<DisplayAmount> oColDel = null;
switch (sortExpression)
{
case "DisplayName":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareName);
ocol.Sort(oColDel);
break;
case "DisplayValue":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareValue);
ocol.Sort(oColDel);
break;
case "TransactionDescription":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareTransactionDescription);
ocol.Sort(oColDel);
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<DisplayAmount>"
as I do with my "DisplayListCollection".
I don't have to do:
List<DisplayAmount> d1 = null;
in my code. I put this in my class:
public class DisplayAmountCollection : List<DisplayAmount> { }
and then call it in my program as:
DisplayAmountCollection dc = null;
But I can't seem to the same with my Comparison<t> statement without getting
an error. If I do the following:
public class DisplayAmountSort : Comparison<DisplayAmount> { }
I get the error:
cannot derive from sealed type 'System.Comparison<DisplayAmount>'
Why can I do it with List but not with Comparison?
Thanks,
Tom