#region 按其他字段重新排序的比较器
/// <summary>
/// 按其他字段重新排序的比较器
/// </summary>
class ListViewItemComparer : IComparer 
{
private int col;
private SortOrder order;
public ListViewItemComparer() 
{
col = 0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order) 
{
col=column;
this.order = order;
}
public int Compare(object x, object y) 
{
int returnVal= -1;
if ( ( ( ListViewItem ) x ).SubItems.Count > col && ( ( ListViewItem ) y ).SubItems.Count > col )
{
returnVal = String.Compare( ( ( ListViewItem ) x ).SubItems[ col ].Text,
( ( ListViewItem )y ).SubItems[ col ].Text);
if( order == SortOrder.Descending )
{
returnVal *= -1;
}
}
return returnVal;
}
}
#endregion #region 排序按钮
/// <summary>
/// 列表框的排序按钮
/// </summary>
class ListViewArrowObject:System.Windows.Forms.Control
{
public ListViewArrowObject(int X,int Y)
{
this.TabStop=false;
this.Location=new Point(X,Y);
this.BackColor=DefaultBackColor;
DrawArrow(this.CreateGraphics());
this.BringToFront();
}

private void DrawArrow(Graphics g)
{
if ( this.Parent == null ) return;
if ( (( icListView )this.Parent ).Sorting == SortOrder.Ascending )
{
DrawUpArrow( this.CreateGraphics(),new Rectangle( 0,0,this.Width,this.Height ) );
}
else
{
DrawDownArrow( this.CreateGraphics(),new Rectangle( 0,0,this.Width,this.Height ) );
}
} private void DrawUpArrow( Graphics g, Rectangle rc )
{
int xTop = rc.Left + rc.Width / 2;
int yTop = ( rc.Height - 4 ) / 2;
            
int xLeft = xTop - 4;
int yLeft = yTop + 4;
            
int xRight = xTop + 4;
int yRight = yTop + 4;

using ( Pen p = new Pen( SystemColors.ControlDarkDark ) )
{
g.DrawLine( p, xLeft, yLeft, xTop, yTop ); //左弦
}
using ( Pen p = new Pen( Color.White ) )
{
g.DrawLine(p, xRight, yRight, xTop, yTop);  //右弦
}
using ( Pen p = new Pen(Color.White) )   //底边
{
g.DrawLine(p, xLeft, yLeft, xRight, yRight);
}
}

private void DrawDownArrow(Graphics g, Rectangle rc)
{
int xBottom = rc.Left + rc.Width/2;
            
int xLeft = xBottom - 4;
int yLeft = (rc.Height - 4)/2;;
            
int xRight = xBottom + 4;
int yRight = (rc.Height - 4)/2;

int yBottom = yRight + 4;

using ( Pen p = new Pen(SystemColors.ControlDarkDark) )
{
g.DrawLine(p, xLeft, yLeft, xBottom, yBottom);
}
using ( Pen p = new Pen(Color.White) )
{
g.DrawLine(p, xRight, yRight, xBottom, yBottom);
}
using ( Pen p = new Pen(SystemColors.ControlDarkDark) )
{
g.DrawLine(p, xLeft, yLeft, xRight, yRight);
}

} protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
DrawArrow(e.Graphics);
} }
#endregion protected override void OnColumnClick(System.Windows.Forms.ColumnClickEventArgs e)
{
SaveColumnWidth();
if (this.SortColumn!=-1)
{
this.Columns[this.SortColumn].Text=this.Columns[this.SortColumn].Text.Trim();
}
if (e.Column != sortColumn)
{
// 设置新的排序列
SortColumn = e.Column;
// 设置排序规则
this.Sorting = SortOrder.Ascending;
}
else
{
if (this.Sorting == SortOrder.Ascending)
this.Sorting = SortOrder.Descending;
else
this.Sorting = SortOrder.Ascending;
}

this.Sort();

this.ListViewItemSorter = new ListViewItemComparer(e.Column,this.Sorting);
AddListArrow(); }

解决方案 »

  1.   

    这两个是有用的,谢了,算账 #region 按其他字段重新排序的比较器
    /// <summary>
    /// 按其他字段重新排序的比较器
    /// </summary>
    class ListViewItemComparer : IComparer 
    {
    private int col;
    private SortOrder order;
    public ListViewItemComparer() 
    {
    col = 0;
    order = SortOrder.Ascending;
    }
    public ListViewItemComparer(int column, SortOrder order) 
    {
    col=column;
    this.order = order;
    }
    public int Compare(object x, object y) 
    {
    int returnVal= -1;
    if ( ( ( ListViewItem ) x ).SubItems.Count > col && ( ( ListViewItem ) y ).SubItems.Count > col )
    {
    returnVal = String.Compare( ( ( ListViewItem ) x ).SubItems[ col ].Text,
    ( ( ListViewItem )y ).SubItems[ col ].Text);
    if( order == SortOrder.Descending )
    {
    returnVal *= -1;
    }
    }
    return returnVal;
    }
    }
    #endregion
    this.ListViewItemSorter = new ListViewItemComparer(e.Column,this.Sorting);