首页 新闻 会员 周边 捐助

C# Winform 的ListView列排序功能

0
悬赏园豆:10 [已解决问题] 解决于 2011-12-31 19:47

请教高手们,我在做winform开发的时候,所有的列表信息都是用的ListView这个控件,然后我又写了一个关于ListView排序功能的类ListViewColumnSorter.我该怎样在这个类里面写ListView的ColumnClick事件呢,而且我每个窗口都是继承了BaseForm这个主窗体,又不能实现继承ListViewColumnSorter这个类了,不能多继承的.

这个那个排序类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Apparel.Common
{
    public class ListViewColumnSorter : IComparer
    {
        private int ColumnToSort;// 指定按照哪个列排序     
        private System.Windows.Forms.SortOrder OrderOfSort;// 指定排序的方式              
        private CaseInsensitiveComparer ObjectCompare;// 声明CaseInsensitiveComparer类对象,
        public ListViewColumnSorter()// 构造函数
        {          
            ColumnToSort = 0;// 默认按第一列排序           
            OrderOfSort = System.Windows.Forms.SortOrder.None;// 排序方式为不排序           
            ObjectCompare = new CaseInsensitiveComparer();// 初始化CaseInsensitiveComparer类对象
        }   
        // 重写IComparer接口.       
        // <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {   int compareResult;
            ListViewItem listviewX, listviewY;
            // 将比较对象转换为ListViewItem对象
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;
            // 比较
            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {   // 因为是正序排序,所以直接返回结果
                return compareResult;
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {  // 如果是反序排序,所以要取负值再返回
                return (-compareResult);
            }
            else
            {
                // 如果相等返回0
                return 0;
            }
        }      
        /// 获取或设置按照哪一列排序.       
        public int SortColumn
        {       
            set
            {    
                ColumnToSort = value;
            }
            get
            {
                return ColumnToSort;
            }
        }      
        /// 获取或设置排序方式.   
        public System.Windows.Forms.SortOrder Order
        {    set
            {
                OrderOfSort = value;
            }
            get
            {
                return OrderOfSort;
            }
        }

     }
}

 

 

这是我使用的ListView控件的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Apparel.BLLOrder;
using Apparel.Model.Order;
using Apparel.BLL.Order;
using Apparel.Model;
using DevComponents.DotNetBar;
using Apparel.BLL.Produce;
using Apparel.Model.Produce;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Collections;
using Apparel.WinFormUI.Produce;
using System.Data.SqlClient;
using System.Diagnostics;
using Apparel.Common;

namespace Apparel.WinFormUI.Order
{
    public partial class OrderList : BaseForm
    {
        #region 全局变量或对象
        // 订单服务业务类实体
        private readonly OrderBF _orderBF = new OrderBF();
        private IList<OrderBE> _orders = new List<OrderBE>();
        private SampleClothesBF _SamCloBF = new SampleClothesBF();//打样的逻辑层实例化
        public static ProduceList ProduceList { get; set; }
        #endregion
        private ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();

        #region 构造涵数
        public OrderList()
        {
            InitializeComponent();
            this.lvOrder.ListViewItemSorter = lvwColumnSorter;
            OrderPaging.PageSize = 20;
            OrderPaging.PagingEvent += LoadAllData;
        }
        #endregion

        #region 加载所有的订单信息
        /// <summary>
        /// 加载所有的订单信息
        /// </summary>
        public void LoadAllData()
        {
            PagingEventEntry pEntry = Common.BuildPagingEntry(OrderPaging.PagingEntry, null);
            try
            {
                _orders = _orderBF.FetchAllOrder(pEntry);
                Bind2ListView();
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region 加载订单数据至ListView
        /// <summary>
        /// 将数据绑定到ListView中
        /// </summary>
        private void Bind2ListView()
        {
            if (null == _orders || _orders.Count == 0)
            {
                return;
            }
            if (lvOrder.Items.Count > 0)
            {
                lvOrder.Items.Clear();
            }
            foreach (var order in _orders)
            {
                var item = lvOrder.Items.Add(order.Employee.ToString());
                item.Tag = order.Key;
                item.SubItems.Add(order.StyleNo);
                item.SubItems.Add(order.CommodityName);
                //item.SubItems.Add(order.ArrangeId.ToString());
                item.SubItems.Add(order.CustomerId.ToString());
                item.SubItems.Add(order.TraceId.ToString());
                item.SubItems.Add(order.SampleClothesFactory);
                item.SubItems.Add(order.BulkProductionFactory);
                item.SubItems.Add(string.Format("{0:C}", order.PriceRMB));
                item.SubItems.Add(order.OrderCount.ToString());
                if (order.RequireDueDate.HasValue)
                {
                    item.SubItems.Add(order.RequireDueDate.Value.ToShortDateString());
                }
                else
                {
                    item.SubItems.Add(String.Empty);
                }

                if (order.PlanDueDate.HasValue)
                {
                    item.SubItems.Add(order.PlanDueDate.Value.ToShortDateString());
                }
                else
                {
                    item.SubItems.Add(String.Empty);
                }

                if (order.ActualDueDate.HasValue && order.ActualDueDate != Common.DefaultDate)
                {
                    item.SubItems.Add(order.ActualDueDate.Value.ToShortDateString());
                }
                else
                {
                    item.SubItems.Add(String.Empty);
                }

                item.SubItems.Add(order.Remark);
            }
        }
        #endregion

        #region 首页加载
        private void OrderList_Load(object sender, EventArgs e)
        {
            //LoadAllData();
            if (Form_Main.dicForm.ContainsKey("生产列表"))
            {
                ProduceList = Form_Main.dicForm["生产列表"] as ProduceList;
            }
        }
        #endregion

       

        private void lvOrder_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // 检查点击的列是不是现在的排序列.
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // 重新设置此列的排序方法.
                if (lvwColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
                }
            }
            else
            {
                // 设置排序列,默认为正向排序
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
            }
            // 用新的排序方法对ListView排序
            this.lvOrder.Sort();
        }
    }
}

 

 

我现在就是想我只要继承了这个类,那我所有的Listview这个控件就都有点击列头排序的功能了.

路过春秋的主页 路过春秋 | 初学一级 | 园豆:10
提问于:2011-12-03 15:31
< >
分享
最佳答案
0

用组合把这个类加到你需要的类中,然后需要的时候实例化调用其中的方法

收获园豆:10
diorlv | 菜鸟二级 |园豆:259 | 2011-12-27 11:48
其他回答(1)
0

谢谢你的回答,最后我自己解决了。

路过春秋 | 园豆:10 (初学一级) | 2011-12-31 19:47

请问你是怎么解决的?

支持(0) 反对(0) 梅西罗纳尔多 | 园豆:200 (初学一级) | 2018-11-21 16:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册