有数据绑定、排序、查找功能的ListView(二):ListViewEx

类别:.NET开发 点击:0 评论:0 推荐:

using System;
using System.Data;
using System.Text;
using System.Globalization;
using System.Collections;
using System.Reflection;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;

using System.Windows.Forms;
using System.Windows.Forms.Design;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;

using System.Runtime.InteropServices;

namespace CommonFrame.OCX
{
 /// <summary>
 /// 为ListView增加数据绑定、查找和排序功能
 /// </summary>
 ///

 #region 枚举
 public enum SortType
 {
  String,
  Number,
  Date
 }
 #endregion

 #region 继承实现ListView列表类
 public class ListViewEx : System.Windows.Forms.ListView
 {

  #region 列表项目所需的WIndows32 API定义

  [StructLayout(LayoutKind.Sequential)]
  private struct HDITEM
  {
   public Int32     mask;
   public Int32     cxy; 
   [MarshalAs(UnmanagedType.LPTStr)]
   public String    pszText;
   public IntPtr  hbm;
   public Int32     cchTextMax;
   public Int32     fmt;
   public Int32     lParam;
   public Int32     iImage;
   public Int32     iOrder;
  };

  [StructLayout(LayoutKind.Sequential) ]
  public struct LVFINDINFO
  {
   public LVFI_FLAGS flags;
   public string psz;
   public IntPtr lParam;
   public Point pt;
   public VirtualKey vkDirection ;
  }

  public enum LVFI_FLAGS
  {
   LVFI_PARAM     = 0x1,
   LVFI_PARTIAL   = 0x8,
   LVFI_STRING    = 0x2,
   LVFI_WRAP    = 0x20,
   LVFI_NEARESTXY = 0x40,
  }

  public enum VirtualKey
  {
   VK_LEFT  = 0x25,
   VK_RIGHT = 0x27,
   VK_UP    = 0x26,
   VK_DOWN  = 0x28,
   VK_HOME  = 0x24,
   VK_END   = 0x23,
   VK_PRIOR = 0x21,
   VK_NEXT  = 0x22
  }

  [DllImport("user32")]
  static extern IntPtr SendMessage(IntPtr Handle, Int32 msg, IntPtr wParam, IntPtr lParam);
  [DllImport("user32", EntryPoint="SendMessage")]
  static extern IntPtr SendMessageH(IntPtr Handle, Int32 msg, IntPtr wParam, ref HDITEM lParam);
  [DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto)]
  static extern int SendMessageL(System.IntPtr hWnd, Int32 msg, int wParam, ref LVFINDINFO lparam);

  const Int32 HDI_WIDTH      = 0x0001;
  const Int32 HDI_HEIGHT     = HDI_WIDTH;
  const Int32 HDI_TEXT       = 0x0002;
  const Int32 HDI_FORMAT     = 0x0004;
  const Int32 HDI_LPARAM     = 0x0008;
  const Int32 HDI_BITMAP     = 0x0010;
  const Int32 HDI_IMAGE      = 0x0020;
  const Int32 HDI_DI_SETITEM = 0x0040;
  const Int32 HDI_ORDER      = 0x0080;
  const Int32 HDI_FILTER     = 0x0100;  // 0x0500

  const Int32 HDF_LEFT            = 0x0000;
  const Int32 HDF_RIGHT           = 0x0001;
  const Int32 HDF_CENTER          = 0x0002;
  const Int32 HDF_JUSTIFYMASK     = 0x0003;
  const Int32 HDF_RTLREADING      = 0x0004;
  const Int32 HDF_OWNERDRAW       = 0x8000;
  const Int32 HDF_STRING          = 0x4000;
  const Int32 HDF_BITMAP          = 0x2000;
  const Int32 HDF_BITMAP_ON_RIGHT = 0x1000;
  const Int32 HDF_IMAGE           = 0x0800;
  const Int32 HDF_SORTUP          = 0x0400;  // 0x0501
  const Int32 HDF_SORTDOWN        = 0x0200;  // 0x0501

  const Int32 LVM_FIRST      = 0x1000;  // List 消息
  const Int32 LVM_FINDITEMA  = LVM_FIRST + 13;
  const Int32 LVM_FINDITEMW  = LVM_FIRST + 83;
  const Int32 LVM_GETHEADER  = LVM_FIRST + 31;

  const Int32 HDM_FIRST        = 0x1200;  // Header 消息
  const Int32 HDM_SETIMAGELIST = HDM_FIRST + 8;
  const Int32 HDM_GETIMAGELIST = HDM_FIRST + 9;
  const Int32 HDM_GETITEM      = HDM_FIRST + 11;
  const Int32 HDM_SETITEM      = HDM_FIRST + 12;

  #endregion

  private string          m_strDataFilter   = "";
  private int             m_intSortColumn   = -1;
  private string          m_strDataMember   = "";
  private CurrencyManager m_objDataManager  = null;
  private DataSet         m_objData         = null;
  private DataView        m_objView         = null;

  internal bool           m_blnModifiedItem = true; 

  private CommonFrame.OCX.ListViewItemsCollection        m_objItems   ;
  private CommonFrame.OCX.ColumnHeaderMappingCollection  m_objMappings ;
  
  public ListViewEx()
  {
   m_objItems    = new CommonFrame.OCX.ListViewItemsCollection(this);
   m_objMappings = new CommonFrame.OCX.ColumnHeaderMappingCollection();
  }

  #region 属性

  [
  CategoryAttribute("Data"),
  BrowsableAttribute(true),
  DescriptionAttribute("设置的数据成员"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible),
  DefaultValueAttribute("")
  ]
  public string DataMember
  {
   get
   {
    return m_strDataMember;
   }
   set
   {
    m_strDataMember = value;
    //this.SetDataMember();
   }
  }

  [
  CategoryAttribute("Data"),
  BrowsableAttribute(true),
  DescriptionAttribute("设置数据源"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible),
  DefaultValueAttribute(null)
  ]
  public DataSet DataSource
  {
   get
   {
    return m_objData;
   }
   set
   {
    m_objData = value;
    this.SetDataSource();
   }
  }

  [
  BrowsableAttribute(false),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)
  ]
  public string DataFilter
  {
   get
   {
    return m_strDataFilter;
   }
   set
   {
    if (this.m_objData != null)
    {
     m_strDataFilter = value;
     this.SetDataFilter();
    }
    else
    {
     m_strDataFilter = "";
    }
   }
  }

  [
  CategoryAttribute("杂项"),
  BrowsableAttribute(true),
  DescriptionAttribute("列表项集合"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content),
  DefaultValueAttribute(null)
  ]
  public CommonFrame.OCX.ListViewItemsCollection Items
  {
   get
   {
    return m_objItems;
   }
  }
                       
  [
  CategoryAttribute("杂项"),
  BrowsableAttribute(true),
  DescriptionAttribute("列表标头与数据成员映射集合"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content),
  DefaultValueAttribute(null)
  ]
  public  CommonFrame.OCX.ColumnHeaderMappingCollection ColumnHeaderMappings
  {
   get
   {
    return m_objMappings;
   }
   set
   {
    m_objMappings = value;
   }
  }

  public bool MultiSelect
  {
   get
   {
    return base.MultiSelect;
   }
   set
   {
    base.MultiSelect = value;
    if ((this.DataSource != null) && (base.MultiSelect == true))
    {
     base.MultiSelect = false;
     throw new Exception("已经设置了数据源DataSource,不能多选");
    }
   }
  }

  #endregion

  #region 方法

  // 填充数据
  private void FillListViewItems()
  {
   ListViewItem item        = null;
   int          intRowCount = this.m_objDataManager.Count ;
   
   this.BeginUpdate();

   //允许修改列表项目
   this.m_blnModifiedItem = true;   
   //清除已经有的列表项目
   if(this.Items.Count>0)
   {
    this.Items.Clear();
   }

   for(int i=0;i<intRowCount;i++)
   {  
    item = new ListViewItem(m_objView[i][this.m_objMappings[0].MappingField].ToString());
    for(int j=1;j<this.m_objMappings.Count;j++)
    {
     item.SubItems.Add(m_objView[i][this.m_objMappings[j].MappingField].ToString());
    }
    this.Items.Add(item);
   }

   this.EndUpdate();
   this.m_blnModifiedItem = false;
  }

  private void SetDataSource()
  {
   if (m_objData != null)
   {
    this.m_blnModifiedItem = true;
    //清空列表项目
    this.Items.Clear();
    //设置多选属性为单选
    this.MultiSelect = false;
    this.m_blnModifiedItem = false;
   }
  }

  private void SetDataMember()
  {
   if ((m_objData == null) || (m_strDataMember == null) ||(m_strDataMember == ""))
   {
    this.Columns.Clear();
    this.ColumnHeaderMappings.Clear();
    return;
   }
   
   DataColumnCollection objDC = m_objData.Tables[m_strDataMember].Columns;

   if (objDC != null)
   {
    this.m_blnModifiedItem = true;
    this.Columns.Clear();   

    for( int i=0;i<objDC.Count;i++)
    {
     ColumnHeader ch = this.Columns.Add(objDC[i].ColumnName.Trim(),60,HorizontalAlignment.Left);
     this.m_objMappings.Add(ch,objDC[i].ColumnName.Trim());
    }

    this.m_blnModifiedItem = false;
   }
  }

  private void SetDataFilter()
  {
   if ( this.DataFilter != m_objView.RowFilter)
   {
    m_objView.RowFilter = this.DataFilter;
   }
  }

  //查找项目
  public ListViewItem FindItem(ListView listView,string itemText,bool includeSubItemsInSearch)
  {
   if (listView.Items.Count == 0)
   {
    return null;
   }

   if (includeSubItemsInSearch == false)
   {
    for(int i=0;i<listView.Items.Count;i++)
    {
     if (listView.Items[i].Text == itemText)
     {
      listView.Items[i].Selected = true;
      listView.Items[i].EnsureVisible();
      return listView.Items[i];
     }
    }
   }
   else
   {
    int intItemIndex    = 0;
    int intSubItemIndex = 0;

    while (intItemIndex < listView.Items.Count)
    {
     intSubItemIndex = 0;
     ListViewItem objItem = listView.Items[intItemIndex];

     while (intSubItemIndex < objItem.SubItems.Count)
     {     
      if (itemText == objItem.SubItems[intSubItemIndex].Text)
      {
       objItem.Selected = true;
       objItem.EnsureVisible();
       return objItem;
      }
      
      intSubItemIndex += 1;
     }
     
     intItemIndex += 1;
    }   
   }
   return null;
  }


  #endregion

  #region 事件

  //在选择列表项后,同步其他数据绑定的控件
  protected override void OnSelectedIndexChanged(EventArgs e)
  {   
   base.OnSelectedIndexChanged (e);

   if ((this.FocusedItem != null) && (m_objDataManager != null))
   {
    m_objDataManager.Position = (int)this.FocusedItem.Index ;
   }
  }
  //获取CurrencyManager,以实现同其他数据绑定控件的同步
  protected override void OnBindingContextChanged(EventArgs e)
  {
   base.OnBindingContextChanged (e);

   if ((this.Parent!= null) && (this.Parent.BindingContext != null) &&
    (m_objData != null) && (m_strDataMember != null) && (m_objDataManager == null))
   {
    m_objDataManager = (CurrencyManager)this.Parent.BindingContext[m_objData,m_strDataMember];
    
    if (m_objDataManager != null)
    {
     this.m_objView = (DataView)m_objDataManager.List;
     m_objDataManager.PositionChanged += new EventHandler(DataSource_PositionChanged);
     m_objDataManager.ItemChanged     += new ItemChangedEventHandler(DataSource_ItemChanged);
    }
   }
  }

  //排序
  protected override void OnColumnClick(ColumnClickEventArgs e)
  {
   base.OnColumnClick (e);

   if(e.Column == m_intSortColumn)
   {
    if (this.Sorting == SortOrder.Ascending)
    {
     this.Sorting = SortOrder.Descending;
    }
    else
    {
     this.Sorting = SortOrder.Ascending;
    }
   }
   else
   {
    m_intSortColumn = e.Column;
    this.Sorting    = SortOrder.Ascending;
   }

   //对于没有进行数据绑定时,使用ListView本身的排序功能
   if (m_objDataManager == null)
   {
    this.ListViewItemSorter = new ListViewItemComparer(m_intSortColumn,this.Sorting,this.m_objMappings[m_intSortColumn].SortType);
    this.Sort();
   }
   //对于进行数据绑定时的排序,使用DataView的排序功能
   else
   {
    string strOrder = "";

    if ( this.Sorting == SortOrder.Ascending)
    {
     strOrder = "Asc";
    }
    else if (this.Sorting == SortOrder.Descending)
    {
     strOrder = "Desc";
    }
    m_objView.Sort = this.m_objMappings[m_intSortColumn].MappingField + " " + strOrder;
   }
  }
  //当其他的数据绑定控件修改当前选项时,ListView同步
  private void DataSource_PositionChanged(object sender,EventArgs e)
  {
   if (this.Items.Count > 0 )
   {
    this.Items[m_objDataManager.Position].Selected = true;  
    this.Items[m_objDataManager.Position].EnsureVisible();
   }
  }

  //当数据源DataSet有内容时,自动填充ListView
  private void DataSource_ItemChanged(object sender ,ItemChangedEventArgs e)
  {
   if (this.DesignMode == false)
   {
    FillListViewItems();

   }
  }
  #endregion
 }

 #endregion
 #region 排序管理类

 public class ListViewItemComparer  : System.Collections.IComparer
 {
  private int           m_intColumn        = 0;
  private SortType      m_enmSortType      = SortType.String;
  private SortOrder     m_enmSortOrder     = SortOrder.Ascending ;

  public ListViewItemComparer (int columnIndex,SortOrder sortOrder)
  {
   m_intColumn    = columnIndex;
   m_enmSortOrder = sortOrder;
  }

  public ListViewItemComparer (int columnIndex,SortOrder sortOrder,SortType sortType)
  {
   m_intColumn    = columnIndex;
   m_enmSortType  = sortType;
   m_enmSortOrder = sortOrder;
  }

  int IComparer.Compare(object x, object y)
  {
   int intSort = 0;

   if(m_enmSortType == SortType.String)           //字符排序
   {
    intSort = String.Compare(((ListViewItem)x).SubItems[m_intColumn].Text.Trim(),
     ((ListViewItem)y).SubItems[m_intColumn].Text.Trim()
     );
   }
   else if (m_enmSortType == SortType.Number)     //数值排序
   {
    try
    {
     System.Double firstNumber =
      Double.Parse(((ListViewItem)x).SubItems[m_intColumn].Text);
     System.Double secondNumber =
      Double.Parse(((ListViewItem)y).SubItems[m_intColumn].Text);
     if (firstNumber > secondNumber)
     {
      intSort = 1;
     }
     else if (firstNumber < secondNumber)
     {
      intSort = -1;
     }
     else
     {
      intSort = 0;
     }
    }
    catch
    {
     intSort = String.Compare(((ListViewItem)x).SubItems[m_intColumn].Text,
      ((ListViewItem)y).SubItems[m_intColumn].Text);
    }
   }
   else if (m_enmSortType == SortType.Date)    //日期排序
   {
    try
    {
     System.DateTime firstDate =
      DateTime.Parse(((ListViewItem)x).SubItems[m_intColumn].Text);
     System.DateTime secondDate =
      DateTime.Parse(((ListViewItem)y).SubItems[m_intColumn].Text);
     intSort = DateTime.Compare(firstDate, secondDate);
    }
    catch
    {
     intSort = String.Compare(((ListViewItem)x).SubItems[m_intColumn].Text,
      ((ListViewItem)y).SubItems[m_intColumn].Text);
    }
   }

   if(m_enmSortOrder == SortOrder.Descending)
   {
    return  intSort*= -1;
   }
   else
   {
    return intSort;
   }
  }
 }

 #endregion

 #region 继承列表项目集合类。当设定DataSource属性时,不允许增加、插入、删除列表项目

 public class ListViewItemsCollection : ListView.ListViewItemCollection
 {
  private CommonFrame.OCX.ListViewEx m_lvwBase   = null;

  public ListViewItemsCollection(CommonFrame.OCX.ListViewEx listView):base(listView)
  {
   m_lvwBase = listView;
  }

  public int Count
  {
   get
   {
    return base.Count;
   }
  }

  public ListViewItem this[int index]
  {
   get
   {
    return base[index];
   }
  }

  public int IndexOf(ListViewItem item)
  {
   return base.IndexOf(item);
  }

  public void Remove(System.Windows.Forms.ListViewItem item)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许删除项目");
   }
   else
   {     
    base.Remove(item);
   }
  }

  public void RemoveAt(int index)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许删除项目");
   }
   else
   {
    base.RemoveAt(index);
   }
  }

  public System.Windows.Forms.ListViewItem Add(string text)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {
    return base.Add(text);
   }
  }
   
  public System.Windows.Forms.ListViewItem Add(string text,int imageIndex)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {
    return base.Add(text,imageIndex);
   }
  }

  public void Insert(int index,System.Windows.Forms.ListViewItem item)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {     
    base.Insert(index,item);
   }
  }

  public void Insert(int index,string text)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {
    base.Insert(index,text);
   }
  }

  public void Insert(int index,string text,int imageIndex)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {
    base.Insert(index,text,imageIndex);
   }
  }

  public void AddRange(System.Windows.Forms.ListViewItem[] values)
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许添加项目");
   }
   else
   {     
    base.AddRange(values);
   }
  }

  public void Clear()
  {
   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
   {
    throw new Exception("已经设置了DataSource,不允许清空项目");
   }
   else
   {     
    base.Clear();
   }
  }
 }

 #endregion

 #region 列标头映射集合类,实现IList接口,对ColumnHeaderMapping类进行管理

 public class ColumnHeaderMappingCollection : System.Collections.IList
 {
  private ArrayList m_arrColumnHearderMapping = new ArrayList(15);

  public ColumnHeaderMappingCollection()
  {
  }
  
  #region 实现IList接口

  int IList.Add(object value)
  {
   return m_arrColumnHearderMapping.Add(value);
  }

  void IList.Clear()
  {
   m_arrColumnHearderMapping.Clear();
  }

  bool IList.Contains(object item)
  {
   return m_arrColumnHearderMapping.Contains(item);
  }

  int IList.IndexOf(object value)
  {
   return m_arrColumnHearderMapping.IndexOf(value);
  }

  void IList.Insert(int index,object value)
  {
   m_arrColumnHearderMapping.Insert(index,value);
  }

  bool IList.IsFixedSize
  {
   get
   {
    return m_arrColumnHearderMapping.IsFixedSize;
   }
  }

  bool IList.IsReadOnly
  {
   get
   {
    return m_arrColumnHearderMapping.IsReadOnly;
   }
  }

  void IList.Remove(object obj)
  {
   m_arrColumnHearderMapping.Remove(obj);
  }

  void IList.RemoveAt(int index)
  {
   m_arrColumnHearderMapping.RemoveAt(index);
  }

  object IList.this[int index]
  {
   get
   {
    return m_arrColumnHearderMapping[index];
   }
   set
   {
   }
  }

  bool  System.Collections.ICollection.IsSynchronized
  {
   get
   {
    return m_arrColumnHearderMapping.IsSynchronized;
   }
  }

  object System.Collections.ICollection.SyncRoot
  {
   get
   {
    return m_arrColumnHearderMapping.SyncRoot;
   }
  }

  void System.Collections.ICollection.CopyTo(System.Array array,int arrayIndex)
  {
   m_arrColumnHearderMapping.CopyTo(array,arrayIndex);
  }

  #endregion

  public int Count
  {
   get
   {
    return m_arrColumnHearderMapping.Count;
   }
  }

  public ColumnHeaderMapping this[int index]
  {
   get
   {
    return (ColumnHeaderMapping)m_arrColumnHearderMapping[index];
   }
  }

  public void Add(ColumnHeader columnHeader,string mappingField)
  {
   m_arrColumnHearderMapping.Add(new ColumnHeaderMapping(columnHeader,mappingField));
  }

  public void Add(ColumnHeaderMapping ColumnHeaderMapping)
  {
   m_arrColumnHearderMapping.Add(ColumnHeaderMapping);
  }

  public void AddRange(ColumnHeaderMapping[] columnHeadMappings)
  {
   m_arrColumnHearderMapping.AddRange(columnHeadMappings);
  }

  public void  AddRange(ColumnHeader[] columnHeaders,string[] mappingFields)
  {
   if (columnHeaders.Length != mappingFields.Length)
   {
    return;
   }
   for(int i=0;i<mappingFields.Length;i++)
   {
    m_arrColumnHearderMapping.Add(new ColumnHeaderMapping(columnHeaders[i],mappingFields[i]));
   }
  }

  public void Remove(ColumnHeaderMapping ColumnHeaderMapping)
  {
   m_arrColumnHearderMapping.Remove(ColumnHeaderMapping);
  }

  public void Clear()
  {
   m_arrColumnHearderMapping.Clear();
  }

  public int IndexOf(ColumnHeaderMapping columnHeaderMapping)
  {
   return m_arrColumnHearderMapping.IndexOf(columnHeaderMapping);
  }

  public void Insert(int index,ColumnHeaderMapping columnHeaderMapping)
  {
   m_arrColumnHearderMapping.Insert(index,columnHeaderMapping);
  }
 
  public void InsertRange(int index,ColumnHeaderMapping[] columnHeaderMappings)
  {
   m_arrColumnHearderMapping.InsertRange(index,columnHeaderMappings);
  }

  public bool Contains(ColumnHeaderMapping columnHeader)
  {
   return m_arrColumnHearderMapping.Contains(columnHeader);
  }

  public System.Collections.IEnumerator GetEnumerator()
  {
   return m_arrColumnHearderMapping.GetEnumerator();
  }
 }
 #endregion

 #region 列表标头映射类,实现列表标头同数据库表字段的映射,定义列表标头在排序时使用的数据类型

 [TypeConverterAttribute(typeof(ColumnHeaderMappingConvert))]
 public class ColumnHeaderMapping
 {
  private ColumnHeader m_objColumnHeader;
  private string       m_strMappingField;
  private SortType     m_enmSortType;

  public ColumnHeaderMapping()
  {
   m_objColumnHeader = new ColumnHeader();
   m_strMappingField = "";
  }

  public ColumnHeaderMapping(ColumnHeader columnHeader,string mappingField)
  {
   m_objColumnHeader = columnHeader;
   m_strMappingField = mappingField;
  }

  [
  CategoryAttribute("杂项"),
  BrowsableAttribute(true),
  DescriptionAttribute("列表标头"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content),
  DefaultValueAttribute(null)
  ]
  public ColumnHeader ColumnHeader
  {
   get
   {
    return m_objColumnHeader;
   }
   set
   {
    m_objColumnHeader = value;
   }
  }

  [
  CategoryAttribute("杂项"),
  BrowsableAttribute(true),
  DescriptionAttribute("映射字段"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible),
  DefaultValueAttribute("")
  ]
  public string MappingField
  {
   get
   {
    return m_strMappingField;
   }
   set
   {
    m_strMappingField = value;
   }
  }

  [
  CategoryAttribute("杂项"),
  BrowsableAttribute(true),
  DescriptionAttribute("列表标头类型"),
  DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible),
  DefaultValueAttribute(SortType.String)
  ]
  public SortType SortType
  {
   get
   {
    return m_enmSortType;
   }
   set
   {
    m_enmSortType = value;
   }
  }
 }
 #endregion

 #region 对ColumnHeaderMapping进行转换,实现ColumnHeaderMapping类在IDE中的序列化

 public class ColumnHeaderMappingConvert : TypeConverter
 {
  public ColumnHeaderMappingConvert()
  {
  }

  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  {
   if (destinationType == typeof(InstanceDescriptor))
   {
    return true;
   }

   return base.CanConvertTo(context, destinationType);
  }

  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  {
   if (value is ColumnHeaderMapping)
   {
    if (destinationType == typeof(InstanceDescriptor))
    {
     object[] properites = new object[2];
     Type[]   types      = new Type[2];

     ColumnHeaderMapping chm = (ColumnHeaderMapping)value;

     types[0]    = typeof(ColumnHeader);
     properites[0] = chm.ColumnHeader ;

     types[1]    = typeof(string);
     properites[1] = chm.MappingField ;

     ConstructorInfo ci = typeof(ColumnHeaderMapping).GetConstructor(types);
     return new InstanceDescriptor(ci,properites);
    }
   }
   return base.ConvertTo(context, culture, value, destinationType);
  }
 }
 #endregion

 #region 继承列表标头集合类(由于继承列表标头集合类后,新增的列表标头不能显示在列表中,所以已经注释,暂时没有使用)
 // public class ColumnHeaderCollection : ListView.ColumnHeaderCollection
 // {
 //  private ListView  m_lvwBase   = null;
 //
 //  public ColumnHeaderCollection(CommonFrame.OCX.ListView listView) : base(listView)
 //  {
 //   m_lvwBase = listView;
 //  }
 //
 //  public void Remove(ColumnHeader item)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许删除项目");
 //   }
 //   else
 //   {     
 //    base.Remove(item);
 //   }
 //  }
 //
 //  public void RemoveAt(int index)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许删除项目");
 //   }
 //   else
 //   {
 //    base.RemoveAt(index);
 //
 //   }
 //  }
 //
 //  public int Add(ColumnHeader value)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许添加项目");
 //   }
 //   else
 //   {
 //    return base.Add(value);
 //   }
 //  }
 //
 //  public ColumnHeader Add(string text,int width,HorizontalAlignment textAlign)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许添加项目");
 //   }
 //   else
 //   {
 //    return base.Add(text,width,textAlign);
 //   }
 //  }
 //
 //  public void Insert(int index,ColumnHeader item)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许添加项目");
 //   }
 //   else
 //   {     
 //    base.Insert(index,item);  
 //   }
 //  }
 //
 //  public void Insert(int index,string text,int width,HorizontalAlignment textAlign)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许添加项目");
 //   }
 //   else
 //   {
 //    base.Insert(index,text,width,textAlign);
 //   }
 //  }
 //
 //  public void AddRange(ColumnHeader[] values)
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许添加项目");
 //   }
 //   else
 //   {     
 //    base.AddRange(values);
 //   }
 //  }
 //
 //  public void Clear()
 //  {
 //   if ((m_lvwBase.DataSource != null) && (m_lvwBase.m_blnModifiedItem == false))
 //   {
 //    throw new Exception("已经设置了DataSource,不允许清空项目");
 //   }
 //   else
 //   {     
 //    base.Clear();
 //   }
 //  }
 //
 //  public ColumnHeader this[int index]
 //  {
 //   get
 //   {
 //    return base[index];
 //   }
 //  }
 // }
 #endregion
}

本文地址:http://com.8s8s.com/it/it42630.htm