#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(); }
/// <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(); }
解决方案 »
- 真奇怪,面向连接方式的数据库修改,不影响表,但是影响显示结果
- 手持设备上,软件运行慢怎么解决?
- C# 如何根据数据库中条件显示文件夹下对应的图片?
- 关于datagrilview的问题
- 怎么查找数据库最大ID并把它赋给一个变量
- 关于netWorkstream
- C#中 ReadPrint函数怎么用
- 表中数据分组,每组读8条
- 控制台程序的标题,用C的话可用system("title abc")更改,不知C#如何才能????
- 做OA软件,现在用什么开发比较理想,微软有没有相应的开发工具,高手指点?
- 怎样实现单击关闭按钮将程序最小化到系统托盘而不关闭程序,在线等
- 教程:如何用C#实现Word里的字体选择下拉列表框
/// <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);