Grzenio Grzenio - 1 year ago 75
C# Question

Create an int column in ListView (Winforms)

Is it possible to create an integer (or DataTime, etc) column in ListView? It is quite important, because I would like to properly sort the list according to this column.

The only way to add subItems to a ListViewItem I found are:

listviewitem.SubItems.Add("1");


I would like to avoid parsing these strings to get the int representation for every sort!

Answer Source

You could use the Tag property. These little helper functions take care of it:

private void setListItem(int row, int column, int value) {
  ListViewItem.ListViewSubItem item = listView1.Items[row].SubItems[column];
  item.Tag = value;
  item.Text = value.ToString();
}
private int getListItem(int row, int column) {
  return (int)listView1.Items[row].SubItems[column].Tag;
}