问一个小问题,ListView的排序问题。

blues-star 2004-07-02 05:20:38
ListViewItem tmp1 = new ListViewItem(new string[]{"13940988402", "您好", System.DateTime.Now.ToString()}, -1);
tmp1.StateImageIndex = 0;

this.GetCurrentList().Items.Add(tmp1);
this.GetCurrentList().Sort();

------------------------
说明:
tmp1是一个ListViewItem(废话),this.GetCurrentList()返回一个ListView,添加tmp1,调用Sort(),如何按照第三个Column时间来排序,

前提:
ListView.View = View.Details;
...全文
152 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qij2256 2004-07-16
  • 打赏
  • 举报
回复
1
blues-star 2004-07-02
  • 打赏
  • 举报
回复
这两个是有用的,谢了,算账



#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);

m777 2004-07-02
  • 打赏
  • 举报
回复
#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();

}

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧