[B][COLOR=#0000FF]// Initialize ListView[/COLOR]
private void InitializeListView()[/B]
{
[COLOR=#0000FF]// Set the view to show details.[/COLOR]
listView1.View = View.Details;
[COLOR=#0000FF]// Allow the user to edit item text.[/COLOR]
listView1.LabelEdit = true;
[COLOR=#0000FF]// Allow the user to rearrange columns.[/COLOR]
listView1.AllowColumnReorder = true;
[COLOR=#0000FF]// Select the item and subitems when selection is made.[/COLOR]
listView1.FullRowSelect = true;
[COLOR=#0000FF]// Display grid lines.[/COLOR]
listView1.GridLines = true;
[COLOR=#0000FF] // Sort the items in the list in ascending order.[/COLOR]
listView1.Sorting = SortOrder.Ascending;
[COLOR=#0000FF]// Attach Subitems to the ListView[/COLOR]
listView1.Columns.Add("Title", 300, HorizontalAlignment.Left);
listView1.Columns.Add("ID", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Price", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Publi-Date", 100, HorizontalAlignment.Left);
[COLOR=#0000FF]// The ListViewItemSorter property allows you to specify the
// object that performs the sorting of items in the ListView.
// You can use the ListViewItemSorter property in combination
// with the Sort method to perform custom sorting.[/COLOR]
[B]_lvwItemComparer = new ListViewItemComparer();
this.listView1.ListViewItemSorter = _lvwItemComparer;[/B]
}
[B]private void LoadList()[/B]
{
[COLOR=#0000FF]// Get the table from the data set[/COLOR]
DataTable dtable = _DataSet.Tables["Titles"];
[COLOR=#0000FF]// Clear the ListView control[/COLOR]
listView1.Items.Clear();
[COLOR=#0000FF]// Display items in the ListView control[/COLOR]
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
[COLOR=#0000FF]// Only row that have not been deleted[/COLOR]
if (drow.RowState != DataRowState.Deleted)
{
[COLOR=#0000FF]// Define the list items[/COLOR]
ListViewItem lvi = new ListViewItem(drow["title"].ToString());
lvi.SubItems.Add (drow["title_id"].ToString());
lvi.SubItems.Add(drow["price"].ToString());
lvi.SubItems.Add(drow["pubdate"].ToString());
[COLOR=#0000FF]// Add the list items to the ListView[/COLOR]
listView1.Items.Add(lvi);
}
}
}
[B][COLOR=#0000FF]// Perform Sorting on Column Headers[/COLOR][/B]
[B]private void listView1_ColumnClick([/B]
object sender,
System.Windows.Forms.ColumnClickEventArgs e)
{
[COLOR=#0000FF]// Determine if clicked column is already the column that is being sorted.[/COLOR]
if (e.Column == _lvwItemComparer.SortColumn)
{
[COLOR=#0000FF]// Reverse the current sort direction for this column.[/COLOR]
if (_lvwItemComparer.Order ==SortOrder.Ascending)
{
_lvwItemComparer.Order = SortOrder.Descending;
}
else
{
_lvwItemComparer.Order = SortOrder.Ascending;
}
}
else
{
[COLOR=#0000FF]// Set the column number that is to be sorted; default to ascending.[/COLOR]
_lvwItemComparer.SortColumn = e.Column;
_lvwItemComparer.Order = SortOrder.Ascending;
}
[COLOR=#0000FF]// Perform the sort with these new sort options.[/COLOR]
this.listView1.Sort();
}
Connect the event-handling method to the [B]ListView[/B] control by adding code to the constructor of your form, as shown in the following example.
[SIZE=2][COLOR=#0000FF]this[/COLOR][/SIZE][SIZE=2].listView1.ColumnClick +=[/SIZE] [SIZE=2][COLOR=#0000FF]new [/COLOR][/SIZE][SIZE=2]System.Windows.Forms.ColumnClickEventHandler(
[/SIZE] [SIZE=2][COLOR=#0000FF]this[/COLOR][/SIZE][SIZE=2].listView1_ColumnClick);[/SIZE]
[COLOR=#0000FF]// This class is an implementation of the 'IComparer' interface.
[/COLOR] [B]public class ListViewItemComparer : IComparer[/B]
{
[COLOR=#0000FF]// Specifies the column to be sorted[/COLOR]
private int ColumnToSort;
[COLOR=#0000FF]// Specifies the order in which to sort (i.e. 'Ascending').[/COLOR]
private SortOrder OrderOfSort;
[COLOR=#0000FF]// Case insensitive comparer object[/COLOR]
private CaseInsensitiveComparer ObjectCompare;
[COLOR=#0000FF] // Class constructor, initializes various elements[/COLOR]
public ListViewItemComparer()
{
[COLOR=#0000FF]// Initialize the column to '0'[/COLOR]
ColumnToSort = 0;
[COLOR=#0000FF]// Initialize the sort order to 'none'[/COLOR]
OrderOfSort = SortOrder.None;
[COLOR=#0000FF]// Initialize the CaseInsensitiveComparer object[/COLOR]
ObjectCompare = new CaseInsensitiveComparer();
}
[COLOR=#0000FF] // This method is inherited from the IComparer interface.
// It compares the two objects passed using a case
// insensitive comparison.
//
// x: First object to be compared
// y: Second object to be compared
//
// The result of the comparison. "0" if equal,
// negative if 'x' is less than 'y' and
// positive if 'x' is greater than 'y'[/COLOR]
[B]public int Compare(object x, object y)[/B]
{
int compareResult;
ListViewItem listviewX, listviewY;
[COLOR=#0000FF]// Cast the objects to be compared to ListViewItem objects[/COLOR]
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
[B][COLOR=#0000FF]//[/COLOR] [COLOR=#0000FF]Case insensitive Compare[/COLOR][/B]
[B]compareResult = ObjectCompare.Compare (
listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text
);[/B]
[COLOR=#0000FF]// Calculate correct return value based on object comparison[/COLOR]
if (OrderOfSort == SortOrder.Ascending)
{
[COLOR=#0000FF]// Ascending sort is selected, return normal result of compare operation[/COLOR]
return compareResult;
}
else if (OrderOfSort ==SortOrder.Descending)
{
[COLOR=#0000FF]// Descending sort is selected, return negative result of compare operation[/COLOR]
return (-compareResult);
}
else
{
[COLOR=#0000FF]// Return '0' to indicate they are equal[/COLOR]
return 0;
}
}
[COLOR=#0000FF]// Gets or sets the number of the column to which to
// apply the sorting operation (Defaults to '0').[/COLOR]
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
[COLOR=#0000FF]// Gets or sets the order of sorting to apply
// (for example, 'Ascending' or 'Descending').[/COLOR]
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
[B]public int Compare(object x, object y)[/B]
{
int compareResult;
ListViewItem listviewX, listviewY;
[COLOR=#0000FF]// Cast the objects to be compared to ListViewItem objects[/COLOR]
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
[COLOR=#0000FF]// Simple String Compare[/COLOR]
[B]compareResult = String.Compare (
listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text
);[/B]
[COLOR=#0000FF]// Calculate correct return value based on object comparison[/COLOR]
if (OrderOfSort == SortOrder.Ascending)
{
[COLOR=#0000FF]// Ascending sort is selected, return normal result of compare operation[/COLOR]
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
[COLOR=#0000FF]// Descending sort is selected, return negative result of compare operation[/COLOR]
return (-compareResult);
}
else
{
[COLOR=#0000FF]// Return '0' to indicate they are equal[/COLOR]
return 0;
}
}
[B]public int Compare(object x, object y)[/B]
{
int compareResult;
ListViewItem listviewX, listviewY;
[COLOR=#0000FF]// Cast the objects to be compared to ListViewItem objects[/COLOR]
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
[COLOR=#0000FF]// Determine whether the type being compared is a date type.[/COLOR]
[B]try[/B]
[B] {[/B]
[B] [COLOR=#0000FF]// Parse the two objects passed as a parameter as a DateTime.[/COLOR][/B]
[B] System.DateTime firstDate = DateTime.Parse(listviewX.SubItems[ColumnToSort].Text);[/B]
[B] System.DateTime secondDate =[/B]
[B] DateTime.Parse(listviewY.SubItems[ColumnToSort].Text);[/B]
[B] [COLOR=#0000FF]// Compare the two dates.[/COLOR][/B]
[B] compareResult = DateTime.Compare(firstDate, secondDate);[/B]
[B] }[/B]
[COLOR=#0000FF]// If neither compared object has a valid date format,[/COLOR]
[COLOR=#0000FF] // perform a Case Insensitive Sort[/COLOR]
[B]catch[/B]
[B] {[/B]
[B] // Case Insensitive Compare[/B]
[B] compareResult = ObjectCompare.Compare ([/B]
[B] listviewX.SubItems[ColumnToSort].Text,[/B]
[B] listviewY.SubItems[ColumnToSort].Text[/B]
[B] );[/B]
[B] }[/B]
[COLOR=#0000FF]// Calculate correct return value based on object comparison[/COLOR]
if (OrderOfSort == SortOrder.Ascending)
{
[COLOR=#0000FF]// Ascending sort is selected, return normal result of compare operation[/COLOR]
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
[COLOR=#0000FF]// Descending sort is selected, return negative result of compare operation[/COLOR]
return (-compareResult);
}
else
{
[COLOR=#0000FF]// Return '0' to indicate they are equal[/COLOR]
return 0;
}
}
ممنون ولی کدهاش چقدر گیج کننده است! مگر نباید از databind استفاده کنیم؟!