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

排序|数据

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
}

时间: 2024-10-27 06:14:46

有数据绑定、排序、查找功能的ListView(二):的相关文章

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

排序|数据 本控件纯粹为练习用,所以没有考虑使用DataGrid代替.该控件不足的地方:1.当父窗体运行后,DataSet被填充时,ListViewEx不能自动判断该种情况,只能通过CurrencyManager的ItemChanged的事件来调用填充ListViewItem的函数,有时该事件会被调用两次,ListViewEx则要填充两次.2.对于FindItem中,按照ListViewItem的Text查找 ListViewItem的方法,记得有一个API可以调用,但是没有实现,只能暂时使用循

jquery实现网页查找功能示例分享

 当需要在页面中查找某个关键字时,一是可以通过浏览器的查找功能实现,二是可以通过前端脚本准确查找定位,本文介绍通过jQuery实现的页面内容查找定位的功能,并可扩展显示查找后的相关信息 本文以查找车站名为例,仿12306官网查找车站售票时间页面效果,当用户输入关键字点击查找按钮或按回车键时,jQuery通过正则匹配内容,准确匹配关键字,并迅速将页面定位滚动到第一个匹配的位置,并显示相关信息(本例中附加信息为车站开始售票时间).   HTML   页面需要放置一个输入框用来输入要查找的关键字,以及

C# DataGrid 数据绑定后怎么实现类似于excel的查找功能,通过某个字段的值找到所在的行

问题描述 应为数据量太大想通过货物名称搜索的方式找到需要采购的某条来输入采购数量,实现的效果就是当输入完货物名称后该货物展现在当前页 解决方案 解决方案二:小弟第一次发帖,希望有大神们支招解决方案三:直接去查询这个结果集啊,比如DataGrid是绑定到list上面的,那么就去这个list里面搜索后更新DataGrid的datacontext就行了解决方案四:用DataGrid的数据源集合去查,包装成一个List<>集合,用linq去查比较方便.DataCollection=DataList.w

用STL的map和算法的find_if实现名表表示和查找功能

问题描述 用STL的map和算法的find_if实现名表表示和查找功能 #include #include #include #include using namespace std; bool older_than_20(int birthdate) { return (2016 - birthdate / 10000)>20; } int main() { maptable_item;//创建一个map类容器,用于存储名表 //创建名表 table_item["Charles"

图书馆图书查找功能~~~我想问下这个函数有没有哪里错~我是渣渣

问题描述 图书馆图书查找功能~~~我想问下这个函数有没有哪里错~我是渣渣 #include "bookrecord.h" void search( BOOK *top){ char choose[25],input[30]; int cn=0; BOOK *p; ASK(p); for(;;) { if(count==0) { printf("这是空表,还没有记录!n"); return ; } printf("1.通过出版社n"); print

C++ 语言关于结构体的排序的功能

问题描述 C++ 语言关于结构体的排序的功能 结构体由多个字段构成,要求按照先后次序对字段排序,用C++语言实现,怎么才能简单高效可靠 解决方案 参考:http://blog.csdn.net/lethic/article/details/7781203 解决方案二: GO语言为结构体排序c++ 结构体排序结构体优先队列排序 解决方案三: 根据结构体中的某个字段排序?

SQLserver中字符串查找功能patindex和charindex的区别_MsSql

最近经常使用字符串查找功能. 包括 1.全匹配查找字符串 2.模糊查找字符串 CHARINDEX 和 PATINDEX 函数都返回指定模式的开始位置.PATINDEX 可使用通配符,而 CHARINDEX 不可以. 这两个函数都带有2个参数: 1 希望获取其位置的模式.使用 PATINDEX,模式是可以包含通配符的字面字符串.使用 CHARINDEX,模式是字面字符串(不能包含通配符). 2 字符串值表达式(通常为列名). 例如,查找模式"wonderful"在 titles 表中 n

silverlight+wcf实现ftp上传出现了此流不支持查找功能

问题描述 FtpWebRequestfilerequest=(FtpWebRequest)FtpWebRequest.Create(newUri(string.Format("ftp://{0}/Test1/{1}",ftpId,file.Name)));filerequest.Credentials=newNetworkCredential("","");if(file.Offset==0){filerequest.Method=WebRequ

SQLserver中字符串查找功能patindex和charindex的区别

最近经常使用字符串查找功能. 包括 1.全匹配查找字符串 2.模糊查找字符串 CHARINDEX 和 PATINDEX 函数都返回指定模式的开始位置.PATINDEX 可使用通配符,而 CHARINDEX 不可以. 这两个函数都带有2个参数: 1 希望获取其位置的模式.使用 PATINDEX,模式是可以包含通配符的字面字符串.使用 CHARINDEX,模式是字面字符串(不能包含通配符). 2 字符串值表达式(通常为列名). 例如,查找模式"wonderful"在 titles 表中 n