Csharp/CSharp Tutorial/GUI Windows Forms/ListView — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:15, 26 мая 2010
Содержание
Fill data from Database to ListView
using System;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
public class DataReaderTest : System.Windows.Forms.Form
{
public DataReaderTest()
{
this.lvCustomers = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
this.lvCustomers.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.lvCustomers.Location = new System.Drawing.Point(8, 8);
this.lvCustomers.Name = "lvCustomers";
this.lvCustomers.Size = new System.Drawing.Size(276, 248);
this.lvCustomers.TabIndex = 0;
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lvCustomers});
this.Load += new System.EventHandler(this.DataReaderTest_Load);
this.ResumeLayout(false);
}
private System.Windows.Forms.ListView lvCustomers;
private string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
private void DataReaderTest_Load(object sender, System.EventArgs e)
{
string SQL = "SELECT * FROM Customers";
lvCustomers.View = View.Details;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(SQL, con);
SqlDataReader r = null;
con.Open();
r = cmd.ExecuteReader();
for (int i = 0; i <= r.FieldCount - 1; i++)
{
lvCustomers.Columns.Add("Column " + (i + 1).ToString(),100, HorizontalAlignment.Left);
}
while (r.Read())
{
ListViewItem lvItem = new ListViewItem(r[0].ToString());
for (int i = 1; i <= r.FieldCount - 1; i++)
{
lvItem.SubItems.Add(r[i].ToString());
}
lvCustomers.Items.Add(lvItem);
}
con.Close();
}
public static void Main()
{
Application.Run(new DataReaderTest());
}
}
ListView Drives
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public sealed class Win32
{
public const uint DRIVE_UNKNOWN = 0;
public const uint DRIVE_NO_ROOT_DIR = 1;
public const uint DRIVE_REMOVABLE = 2;
public const uint DRIVE_FIXED = 3;
public const uint DRIVE_REMOTE = 4;
public const uint DRIVE_CDROM = 5;
public const uint DRIVE_RAMDISK = 6;
[DllImport("kernel32.dll")]
public static extern uint GetDriveType(
string lpRootPathName // root directory
);
[DllImport("kernel32.dll")]
private static extern bool GetVolumeInformation(
string lpRootPathName, // Root directory
StringBuilder lpVolumeNameBuffer, // Volume name buffer
int nVolumeNameSize, // Length of name buffer
ref int lpVolumeSerialNumber, // Volume serial number
ref int lpMaximumComponentLength, // Maximum file name length
ref int lpFileSystemFlags, // File system options
StringBuilder lpFileSystemNameBuffer, // File system name buffer
int nFileSystemNameSize // Length of file system name buffer
);
public static string GetVolumeName(string strRootPath)
{
StringBuilder sbVolumeName = new StringBuilder(256);
StringBuilder sbFileSystemName = new StringBuilder(256);
int nVolSerial = 0;
int nMaxCompLength = 0;
int nFSFlags = 0;
bool bResult = GetVolumeInformation(strRootPath,sbVolumeName,256,
ref nVolSerial, ref nMaxCompLength, ref nFSFlags, sbFileSystemName, 256);
if (true == bResult)
{
return sbVolumeName.ToString();
}
else
{
return "";
}
}
}
public class DrivesForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView lvDrives;
private System.Windows.Forms.MainMenu mnuMain;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem mnuLargeIcons;
private System.Windows.Forms.MenuItem mnuSmallIcons;
private System.Windows.Forms.MenuItem mnuList;
private System.Windows.Forms.MenuItem mnuDetails;
public DrivesForm()
{
this.lvDrives = new System.Windows.Forms.ListView();
this.mnuMain = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuLargeIcons = new System.Windows.Forms.MenuItem();
this.mnuSmallIcons = new System.Windows.Forms.MenuItem();
this.mnuList = new System.Windows.Forms.MenuItem();
this.mnuDetails = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// lvDrives
//
this.lvDrives.Dock = System.Windows.Forms.DockStyle.Fill;
this.lvDrives.Name = "lvDrives";
this.lvDrives.Size = new System.Drawing.Size(336, 277);
this.lvDrives.TabIndex = 0;
this.lvDrives.View = System.Windows.Forms.View.List;
this.lvDrives.ItemActivate += new System.EventHandler(this.ItemActivated);
this.lvDrives.SelectedIndexChanged += new System.EventHandler(this.OnSelItemChanged);
//
// mnuMain
//
this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuLargeIcons,
this.mnuSmallIcons,
this.mnuList,
this.mnuDetails});
this.menuItem1.Text = "Style";
//
// mnuLargeIcons
//
this.mnuLargeIcons.Index = 0;
this.mnuLargeIcons.Text = "Large Icons";
this.mnuLargeIcons.Click += new System.EventHandler(this.OnMenuClick);
//
// mnuSmallIcons
//
this.mnuSmallIcons.Index = 1;
this.mnuSmallIcons.Text = "Small Icons";
this.mnuSmallIcons.Click += new System.EventHandler(this.OnMenuClick);
//
// mnuList
//
this.mnuList.Index = 2;
this.mnuList.Text = "List";
this.mnuList.Click += new System.EventHandler(this.OnMenuClick);
//
// mnuDetails
//
this.mnuDetails.Index = 3;
this.mnuDetails.Text = "Details";
this.mnuDetails.Click += new System.EventHandler(this.OnMenuClick);
//
// DrivesForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(336, 277);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lvDrives});
this.Menu = this.mnuMain;
this.Name = "DrivesForm";
this.Text = "Drives on computer";
this.ResumeLayout(false);
// Small and large image lists
lvDrives.SmallImageList = new ImageList();
Bitmap icoSmall = new Bitmap(GetType(), "LvIcons.bmp");
icoSmall.MakeTransparent(Color.FromArgb(0xff, 0x00, 0xff));
lvDrives.SmallImageList.Images.AddStrip(icoSmall);
lvDrives.LargeImageList = new ImageList();
Bitmap icoLarge = new Bitmap(GetType(), "LvIconsLarge.bmp");
icoLarge.MakeTransparent(Color.FromArgb(0xff, 0x00, 0xff));
Size sizeImages = new Size(32,32);
lvDrives.LargeImageList.ImageSize = sizeImages;
lvDrives.LargeImageList.Images.AddStrip(icoLarge);
lvDrives.Columns.Add("Drive", 100, HorizontalAlignment.Left);
lvDrives.Columns.Add("Type", 150, HorizontalAlignment.Left);
ListDrives();
}
protected void ListDrives()
{
string[] drives = Directory.GetLogicalDrives();
string strDriveType = "";
for (int i=0; i < drives.Length; i++)
{
string strDriveName = Win32.GetVolumeName(drives[i]);
ListViewItem lvi = new ListViewItem();
// Note: Network drives show up as local.
switch(Win32.GetDriveType(drives[i]))
{
case Win32.DRIVE_CDROM:
strDriveType = "Compact Disc";
lvi.ImageIndex = 1;
break;
case Win32.DRIVE_FIXED:
strDriveType = "Local Disc";
lvi.ImageIndex = 0;
break;
case Win32.DRIVE_REMOVABLE:
strDriveType = "Floppy";
lvi.ImageIndex = 2;
break;
default:
goto case Win32.DRIVE_FIXED;
}
if (0 == strDriveName.Length) strDriveName = strDriveType;
lvi.Text = strDriveName + " (" + drives[i].Substring(0, 2) + ")";
lvi.SubItems.Add(strDriveType);
this.lvDrives.Items.Add(lvi);
}
}
static void Main()
{
Application.Run(new DrivesForm());
}
private void OnMenuClick(object sender, System.EventArgs e)
{
MenuItem mnu = (MenuItem)sender;
switch(mnu.Text)
{
case "Large Icons": lvDrives.View = View.LargeIcon; break;
case "Small Icons": lvDrives.View = View.SmallIcon; break;
case "List": lvDrives.View = View.List; break;
default: lvDrives.View = View.Details; break;
}
}
private void OnSelItemChanged(object sender, System.EventArgs e)
{
string strSelected = "";
foreach (ListViewItem lvi in lvDrives.SelectedItems)
{
strSelected += " " + lvi.Text;
}
MessageBox.Show(strSelected);
}
private void ItemActivated(object sender, System.EventArgs e)
{
MessageBox.Show("Item activated");
}
}
ListView for File and Folder
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
public class Form1 : Form
{
private System.Collections.Specialized.StringCollection folderCol;
public Form1()
{
InitializeComponent();
folderCol = new System.Collections.Specialized.StringCollection();
CreateHeadersAndFillListView();
PaintListView(@"C:\");
folderCol.Add(@"C:\");
}
private void CreateHeadersAndFillListView()
{
ColumnHeader colHead;
colHead = new ColumnHeader();
colHead.Text = "Filename";
this.listViewFilesAndFolders.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Size";
this.listViewFilesAndFolders.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Last accessed";
this.listViewFilesAndFolders.Columns.Add(colHead);
}
private void PaintListView(string root)
{
try
{
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
if (root.rupareTo("") == 0)
return;
DirectoryInfo dir = new DirectoryInfo(root);
DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
this.listViewFilesAndFolders.Items.Clear();
this.labelCurrentPath.Text = root;
this.listViewFilesAndFolders.BeginUpdate();
foreach (DirectoryInfo di in dirs)
{
lvi = new ListViewItem();
lvi.Text = di.Name;
lvi.Tag = di.FullName;
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = "";
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = di.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
this.listViewFilesAndFolders.Items.Add(lvi);
}
foreach (FileInfo fi in files)
{
lvi = new ListViewItem();
lvi.Text = fi.Name;
lvi.Tag = fi.FullName;
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.Length.ToString();
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
this.listViewFilesAndFolders.Items.Add(lvi);
}
this.listViewFilesAndFolders.EndUpdate();
}
catch (System.Exception err)
{
MessageBox.Show("Error: " + err.Message);
}
}
private void listViewFilesAndFolders_ItemActivate(object sender, EventArgs e)
{
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
string filename = lw.SelectedItems[0].Tag.ToString();
if (lw.SelectedItems[0].ImageIndex != 0)
{
try
{
System.Diagnostics.Process.Start(filename);
}
catch
{
return;
}
}
else
{
PaintListView(filename);
folderCol.Add(filename);
}
}
private void buttonBack_Click(object sender, EventArgs e)
{
if (folderCol.Count > 1)
{
PaintListView(folderCol[folderCol.Count - 2].ToString());
folderCol.RemoveAt(folderCol.Count - 1);
}
else
{
PaintListView(folderCol[0].ToString());
}
}
private void radioButtonLargeIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.LargeIcon;
}
private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.SmallIcon;
}
private void radioButtonList_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.List;
}
private void radioButtonDetails_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Details;
}
private void radioButtonTile_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Tile;
}
private void InitializeComponent()
{
this.labelCurrentPath = new System.Windows.Forms.Label();
this.listViewFilesAndFolders = new System.Windows.Forms.ListView();
this.groupBoxViewMode = new System.Windows.Forms.GroupBox();
this.radioButtonTile = new System.Windows.Forms.RadioButton();
this.radioButtonDetails = new System.Windows.Forms.RadioButton();
this.radioButtonList = new System.Windows.Forms.RadioButton();
this.radioButtonSmallIcon = new System.Windows.Forms.RadioButton();
this.radioButtonLargeIcon = new System.Windows.Forms.RadioButton();
this.buttonBack = new System.Windows.Forms.Button();
this.groupBoxViewMode.SuspendLayout();
this.SuspendLayout();
//
this.labelCurrentPath.Location = new System.Drawing.Point(12, 9);
this.labelCurrentPath.Name = "labelCurrentPath";
this.labelCurrentPath.Size = new System.Drawing.Size(429, 13);
this.labelCurrentPath.TabIndex = 0;
//
this.listViewFilesAndFolders.LargeImageList = this.imageListLarge;
this.listViewFilesAndFolders.Location = new System.Drawing.Point(12, 25);
this.listViewFilesAndFolders.Size = new System.Drawing.Size(429, 208);
this.listViewFilesAndFolders.UseCompatibleStateImageBehavior = false;
this.listViewFilesAndFolders.View = System.Windows.Forms.View.Details;
this.listViewFilesAndFolders.ItemActivate += new System.EventHandler(this.listViewFilesAndFolders_ItemActivate);
//
this.groupBoxViewMode.Controls.Add(this.radioButtonTile);
this.groupBoxViewMode.Controls.Add(this.radioButtonDetails);
this.groupBoxViewMode.Controls.Add(this.radioButtonList);
this.groupBoxViewMode.Controls.Add(this.radioButtonSmallIcon);
this.groupBoxViewMode.Controls.Add(this.radioButtonLargeIcon);
this.groupBoxViewMode.Location = new System.Drawing.Point(447, 25);
this.groupBoxViewMode.Name = "groupBoxViewMode";
this.groupBoxViewMode.Size = new System.Drawing.Size(105, 208);
this.groupBoxViewMode.TabIndex = 2;
this.groupBoxViewMode.TabStop = false;
this.groupBoxViewMode.Text = "View Mode";
//
// radioButtonTile
//
this.radioButtonTile.AutoSize = true;
this.radioButtonTile.Location = new System.Drawing.Point(6, 111);
this.radioButtonTile.Name = "radioButtonTile";
this.radioButtonTile.Size = new System.Drawing.Size(42, 17);
this.radioButtonTile.TabIndex = 4;
this.radioButtonTile.Text = "Tile";
this.radioButtonTile.UseVisualStyleBackColor = true;
this.radioButtonTile.CheckedChanged += new System.EventHandler(this.radioButtonTile_CheckedChanged);
//
// radioButtonDetails
//
this.radioButtonDetails.AutoSize = true;
this.radioButtonDetails.Checked = true;
this.radioButtonDetails.Location = new System.Drawing.Point(6, 88);
this.radioButtonDetails.Name = "radioButtonDetails";
this.radioButtonDetails.Size = new System.Drawing.Size(57, 17);
this.radioButtonDetails.TabIndex = 3;
this.radioButtonDetails.TabStop = true;
this.radioButtonDetails.Text = "Details";
this.radioButtonDetails.UseVisualStyleBackColor = true;
this.radioButtonDetails.CheckedChanged += new System.EventHandler(this.radioButtonDetails_CheckedChanged);
//
// radioButtonList
//
this.radioButtonList.AutoSize = true;
this.radioButtonList.Location = new System.Drawing.Point(6, 65);
this.radioButtonList.Name = "radioButtonList";
this.radioButtonList.Size = new System.Drawing.Size(41, 17);
this.radioButtonList.TabIndex = 2;
this.radioButtonList.Text = "List";
this.radioButtonList.UseVisualStyleBackColor = true;
this.radioButtonList.CheckedChanged += new System.EventHandler(this.radioButtonList_CheckedChanged);
//
// radioButtonSmallIcon
//
this.radioButtonSmallIcon.AutoSize = true;
this.radioButtonSmallIcon.Location = new System.Drawing.Point(6, 42);
this.radioButtonSmallIcon.Name = "radioButtonSmallIcon";
this.radioButtonSmallIcon.Size = new System.Drawing.Size(74, 17);
this.radioButtonSmallIcon.TabIndex = 1;
this.radioButtonSmallIcon.Text = "Small Icon";
this.radioButtonSmallIcon.UseVisualStyleBackColor = true;
this.radioButtonSmallIcon.CheckedChanged += new System.EventHandler(this.radioButtonSmallIcon_CheckedChanged);
//
// radioButtonLargeIcon
//
this.radioButtonLargeIcon.AutoSize = true;
this.radioButtonLargeIcon.Location = new System.Drawing.Point(6, 19);
this.radioButtonLargeIcon.Name = "radioButtonLargeIcon";
this.radioButtonLargeIcon.Size = new System.Drawing.Size(76, 17);
this.radioButtonLargeIcon.TabIndex = 0;
this.radioButtonLargeIcon.Text = "Large Icon";
this.radioButtonLargeIcon.UseVisualStyleBackColor = true;
this.radioButtonLargeIcon.CheckedChanged += new System.EventHandler(this.radioButtonLargeIcon_CheckedChanged);
//
// buttonBack
//
this.buttonBack.Location = new System.Drawing.Point(275, 240);
this.buttonBack.Name = "buttonBack";
this.buttonBack.Size = new System.Drawing.Size(75, 23);
this.buttonBack.TabIndex = 3;
this.buttonBack.Text = "Back";
this.buttonBack.UseVisualStyleBackColor = true;
this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
//
// imageListSmall
//
this.imageListSmall.TransparentColor = System.Drawing.Color.Transparent;
this.imageListSmall.Images.SetKeyName(0, "Folder 16x16.ICO");
this.imageListSmall.Images.SetKeyName(1, "Text 16x16.ICO");
//
// imageListLarge
//
this.imageListLarge.TransparentColor = System.Drawing.Color.Transparent;
this.imageListLarge.Images.SetKeyName(0, "Folder32x32.ICO");
this.imageListLarge.Images.SetKeyName(1, "Text 32x32.ICO");
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(564, 273);
this.Controls.Add(this.buttonBack);
this.Controls.Add(this.groupBoxViewMode);
this.Controls.Add(this.listViewFilesAndFolders);
this.Controls.Add(this.labelCurrentPath);
this.Name = "Form1";
this.Text = "ListView";
this.groupBoxViewMode.ResumeLayout(false);
this.groupBoxViewMode.PerformLayout();
this.ResumeLayout(false);
}
private System.Windows.Forms.Label labelCurrentPath;
private System.Windows.Forms.ListView listViewFilesAndFolders;
private System.Windows.Forms.GroupBox groupBoxViewMode;
private System.Windows.Forms.RadioButton radioButtonTile;
private System.Windows.Forms.RadioButton radioButtonDetails;
private System.Windows.Forms.RadioButton radioButtonList;
private System.Windows.Forms.RadioButton radioButtonSmallIcon;
private System.Windows.Forms.RadioButton radioButtonLargeIcon;
private System.Windows.Forms.Button buttonBack;
private System.Windows.Forms.ImageList imageListSmall;
private System.Windows.Forms.ImageList imageListLarge;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
ListView Sorter
using System;
using System.Collections;
using System.Windows.Forms;
class FolderHolder: Form
{
public FolderHolder()
{
InitializeComponent();
}
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
ListViewItemComparer sorter = listView1.ListViewItemSorter as ListViewItemComparer;
if (sorter == null)
{
sorter = new ListViewItemComparer(e.Column);
listView1.ListViewItemSorter = sorter;
}
else
{
sorter.Column = e.Column;
}
listView1.Sort();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new FolderHolder());
}
private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"1","B","A"}, -1);
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {"2","A","D"}, -1);
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {"4","C","B"}, -1);
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {this.columnHeader1,this.columnHeader2,this.columnHeader3});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.GridLines = true;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {listViewItem1,listViewItem2,listViewItem3});
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Size = new System.Drawing.Size(292, 266);
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
//
// FolderHolder
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listView1);
this.ResumeLayout(false);
}
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
}
public class ListViewItemComparer : IComparer
{
private int column;
private bool numeric = false;
public int Column
{
get { return column; }
set { column = value; }
}
public bool Numeric
{
get { return numeric; }
set { numeric = value; }
}
public ListViewItemComparer(int columnIndex)
{
Column = columnIndex;
}
public int Compare(object x, object y)
{
ListViewItem itemX = x as ListViewItem;
ListViewItem itemY = y as ListViewItem;
if (itemX == null && itemY == null)
return 0;
else if (itemX == null)
return -1;
else if (itemY == null)
return 1;
if (itemX == itemY) return 0;
if (Numeric)
{
decimal itemXVal, itemYVal;
if (!Decimal.TryParse(itemX.SubItems[Column].Text, out itemXVal))
{
itemXVal = 0;
}
if (!Decimal.TryParse(itemY.SubItems[Column].Text, out itemYVal))
{
itemYVal = 0;
}
return Decimal.rupare(itemXVal, itemYVal);
} else {
string itemXText = itemX.SubItems[Column].Text;
string itemYText = itemY.SubItems[Column].Text;
return String.rupare(itemXText, itemYText);
}
}
}