c#的listview控件是怎么对某一列排序的?

qianniao08 2008-12-06 10:18:33
c#的listview控件是怎么对某一列排序的?
...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp_shy 2008-12-06
  • 打赏
  • 举报
回复
自己做一个排序类来实现,类代码如下

class ListViewItemComparer : IComparer
{
private int nCol;
private bool bAscend;
public ListViewItemComparer()
{
nCol = 0;
}
public ListViewItemComparer(int nColumn, bool bAscend)
{
this.nCol = nColumn;
this.bAscend = bAscend;
}
public int Compare(object x, object y)
{
int nCompare = String.Compare(((ListViewItem)x).SubItems[nCol].Text, ((ListViewItem)y).SubItems[nCol].Text);
return this.bAscend ? nCompare : - nCompare;
}
}


使用方法在ListView使用窗体的代码中定义一个bool型全局变量
private bool bLvImage = false;//控制排序的bool型全局变量
然后处理在ListView的ColumnClick事件

private void ListView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
bLvImage = !bLvImage;
this.ListView1.ListViewItemSorter = new ListViewItemComparer(e.Column, bLvImage);
}
gomoku 2008-12-06
  • 打赏
  • 举报
回复
要自己写。

建议用DataGridView来做,DataGridView内置支持对某一列排序。外观上把它设置的跟ListView一样:


dataGridView1.ReadOnly = true;
dataGridView1.RowHeadersVisible = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

110,539

社区成员

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

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

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