ListView控件排序相关

QQ886372 2007-01-15 08:03:34
如何实现像windows资源管理器那样得每一列标头上都有个三角形,向上表示升叙向下表示降序,点击切换排序???
...全文
340 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
金坷垃就 2012-10-28
  • 打赏
  • 举报
回复
mark
Ccode 2007-08-09
  • 打赏
  • 举报
回复
mark
rgwfeng2 2007-06-17
  • 打赏
  • 举报
回复
mark
RedGoldFish 2007-06-17
  • 打赏
  • 举报
回复
LZ说的标头上都有个三角形在控件中是不自带的,需要通过调用API画上去,下边这些代码是我们自己项目中正在用的,你改一下就可以用:

#region Windows API for setting up Listview column header sort icon; get the order

private const UInt32 LVM_GETHEADER = 4127;
private const UInt32 HDM_SETIMAGELIST = 4616;
private const UInt32 LVM_SETCOLUMN = 4122;
private const uint LVCF_FMT = 1;
private const uint LVCF_IMAGE = 16;
private const int LVCFMT_IMAGE = 2048;

public const UInt32 LVM_GETCOLUMN = 4121;
public const UInt32 LVCF_ORDER = 0x0020;

// Define the LVCOLUMN for use with interop
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
private struct LVCOLUMN
{
public uint mask;
public int fmt;
public int cx;
public IntPtr pszText;
public int cchTextMax;
public int iSubItem;
public int iImage;
public int iOrder;
}

// Declare two overloaded SendMessage functions. The
// difference is in the last parameter.
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);

[DllImport("User32", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, ref LVCOLUMN lParam);

//sortIconImageList 中需要包括向下和向上的小图标
public static void SetHeaderSortIcon(ListView listView,
int sortColumnIndex,
ImageList sortIconImageList,
int sortIconIndex)
{
try
{
IntPtr hc;
int i;
// Assign the ImageList to the header control.
// The header control includes all columns.
// Get a handle to the header control.
hc = SendMessage(listView.Handle, LVM_GETHEADER, (UInt32)0, (UInt32)0);

// Add the image list to the header control.
SendMessage(hc, HDM_SETIMAGELIST, (UInt32)0, (UInt32)sortIconImageList.Handle);

// Set the image for each column.
//
// In the following code, we use successive images in the image list to place on
// successive columns in the ColumnHeader by looping through all columns.
//
// The LVCOLUMN is also used to define alignment,
// so by using it here, we are resetting the alignment if it was defined
// in the designer. If you need to set the alignment, you will need to change
// the code below to set it here.
//
for (i = 0; i < listView.Columns.Count; i++)
{

// Use the LVM_SETCOLUMN message to set the column's image index.
LVCOLUMN col;
// col.mask: include LVCF_FMT | LVCF_IMAGE
col.mask = LVCF_FMT | LVCF_IMAGE;

// LVCFMT_IMAGE
col.fmt = LVCFMT_IMAGE;

col.fmt |= 0x1000;

// The image to use from the image list.

if (i == sortColumnIndex)
col.iImage = sortIconIndex;
else
col.iImage = -1; //No image when it is -1

// Initialize the rest to zero.
col.pszText = (IntPtr)0;
col.cchTextMax = 0;
col.cx = 0;
col.iSubItem = 0;
col.iOrder = 0;

// Send the LVM_SETCOLUMN message.
// The column that we are assigning the image to is defined in the third parameter.
SendMessage(listView.Handle, LVM_SETCOLUMN, (UInt32)i, ref col);
}


}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
}


/// <summary>
/// Get the order index (zero based) of a listview column.
/// </summary>
/// <param name="listView"></param>
/// <param name="columnIndex"></param>
/// <returns>returns -1 if failed.</returns>
public static int GetColumnOrder(ListView listView,
int columnIndex)
{
try
{
LVCOLUMN col = new LVCOLUMN();
col.mask = LVCF_ORDER;
SendMessage(listView.Handle, LVM_GETCOLUMN, (UInt32)columnIndex, ref col);
return col.iOrder;
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
return -1;
}
}



#endregion
liujia_0421 2007-01-15
  • 打赏
  • 举报
回复
for example:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.listView1.Items.Add("4");
this.listView1.Items[0].SubItems.Add("aaa");
this.listView1.Items.Add("2");
this.listView1.Items[1].SubItems.Add("bbb");
}

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
}

}
class ListViewItemComparer : System.Collections.IComparer
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
Snega 2007-01-15
  • 打赏
  • 举报
回复
需要自己实现,ListView没有这个功能

110,502

社区成员

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

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

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