this smart listview is multiselect and sortable. i attach the entire project too.
#region Name Spaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.Text.RegularExpressions;
#endregion
namespace SmartControls
{
public partial class SmartListView : ListView
{
#region Constructor
public SmartListView()
{
InitializeComponent();
}
#endregion
#region Properties
private IList dataSource = null;
public IList DataSource
{
get
{
return dataSource;
}
set
{
dataSource = value;
if (dataSource != null)
{
try
{
CreateItems();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private string[] columnHeaders = null;
public string[] ColumnHeaders
{
set
{
columnHeaders = value;
}
}
#endregion
#region Helper Methods
private void CreateItems()
{
Items.Clear();
Columns.Clear();
if (dataSource.Count > 0)
{
BeginUpdate();
PropertyInfo[] properties = dataSource[0].GetType().GetProperties();
if (properties.Length > 0)
{
ColumnHeader ch = new ColumnHeader();
ch.Width = -2;
ch.Text = "#";
Columns.Insert(0, ch);
if (columnHeaders != null)
{
if (columnHeaders.Length == properties.Length)
{
foreach (string columnName in columnHeaders)
{
ch = new ColumnHeader();
ch.Text = columnName;
ch.Width = -2;
Columns.Add(ch);
}
}
else
{
throw new ApplicationException("Column header collection does not match object properties");
}
}
else
{
foreach (PropertyInfo pInfo in properties)
{
ch = new ColumnHeader();
ch.Text = pInfo.Name;
ch.Width = -2;
Columns.Add(ch);
}
}
for (int i = 0; i < dataSource.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = (i + 1).ToString();
for (int propIndex = 0; propIndex < properties.Length; propIndex++)
{
lvi.SubItems.Add(properties[propIndex].GetValue(dataSource[i], null).ToString());
}
Items.Add(lvi);
}
EndUpdate();
}
}
}
#endregion
#region Sorting Functionality
class ListItem : IComparable
{
private string columnValue = string.Empty;
private ListViewItem item = null;
public ListViewItem Item
{
get
{
return item;
}
}
public ListItem(string _columnValue, ListViewItem _item)
{
columnValue = _columnValue;
item = _item;
}
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is ListItem)
{
ListItem li = (ListItem)obj;
return columnValue.CompareTo(li.columnValue);
}
throw new ArgumentException("object is not ListItem");
}
#endregion
}
private List<bool> sortDirections = new List<bool>();
void SmartListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
if (sortDirections.Count == 0)
{
for (int i = 0; i < Columns.Count; i++)
{
sortDirections.Add(true);
}
}
List<ListItem> listitems = new List<ListItem>();
foreach (ListViewItem lvi in Items)
{
listitems.Add(new ListItem(lvi.SubItems[e.Column].Text, lvi));
}
if (sortDirections[e.Column])
{
listitems.Sort();
sortDirections[e.Column] = false;
}
else
{
listitems.Sort();
listitems.Reverse();
sortDirections[e.Column] = true;
}
Items.Clear();
foreach (ListItem li in listitems)
{
Items.Add(li.Item);
}
}
#endregion
}
}
#region Name Spaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
#endregion
namespace SmartControls
{
public partial class SmartListForm : Form
{
#region Properties
public IList DataSource
{
get
{
return smartListView1.DataSource;
}
set
{
if (value != null)
smartListView1.DataSource = value;
else
throw new ApplicationException("DataSource can not be null");
}
}
public string[] ColumnHeaders
{
set
{
smartListView1.ColumnHeaders = value;
}
}
private IList selectedItems = null;
public IList SelectedItems
{
get
{
return selectedItems;
}
set
{
selectedItems = value;
}
}
private bool multiSelect = true;
public bool MultiSelect
{
set
{
multiSelect = value;
smartListView1.CheckBoxes = value;
smartListView1.FullRowSelect = !value;
}
}
private List<string> selectedItemsAsText = new List<string>();
public List<string> SelectedItemsAsText
{
get
{
return selectedItemsAsText;
}
set
{
selectedItemsAsText = value;
}
}
public string SelectedText
{
get
{
return string.Join("\r\n", SelectedItemsAsText.ToArray());
}
}
#endregion
#region Constructor
/// <summary>
/// use this if you want to display items only
/// </summary>
public SmartListForm()
{
InitializeComponent();
}
/// <summary>
/// use this if you want to enable selection of items
/// </summary>
/// <param name="_selectedItems"></param>
public SmartListForm(IList _selectedItems)
{
SelectedItems = _selectedItems;
InitializeComponent();
}
#endregion
#region Event Handlers
private void SmartListForm_Load(object sender, EventArgs e)
{
if (smartListView1.Items.Count > 0)
{
if (multiSelect)
smartListView1.Items[0].Checked = true;
else
smartListView1.Items[0].Selected = true;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (SelectedItems != null)
{
SelectedItems.Clear();
SelectedItemsAsText.Clear();
foreach (ListViewItem lvi in smartListView1.Items)
{
if (multiSelect)
{
if (lvi.Checked)
{
SelectedItems.Add(DataSource[Convert.ToInt32(lvi.Text) - 1]);
SelectedItemsAsText.Add(ListViewItemToSring(lvi));
}
}
else
{
if (lvi.Selected)
{
SelectedItems.Add(DataSource[Convert.ToInt32(lvi.Text) - 1]);
SelectedItemsAsText.Add(ListViewItemToSring(lvi));
}
}
}
}
DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
#endregion
#region Helper Methods
private string ListViewItemToSring(ListViewItem lvi)
{
StringBuilder sb = new StringBuilder();
for (int i = 1; i < lvi.SubItems.Count; i++)
{
sb.Append(" " + lvi.SubItems[i].Text);
}
return sb.ToString();
}
#endregion
}
}