tags:

views:

539

answers:

3

Hi,

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!

+1  A: 

Hi,

Rather than that you just add strings to the list and receive strings by using .Text property.

Example:

    int a = 0;
    listView1.Items.Add(a.ToString());

or

    int a = Convert.ToInt32(listView1.Items[0].SubItems[0].Text);

You can do this same with DataTime and other datatypes, just by converting them to String type.

How you interpret the column containing i.e dates it fully depends of you. Just implement your algorithm of sorting particular ListView column.

Greets Mariusz

aristo
Well ok, but this is really parsing these strings....
Grzenio
As far as I know a particular ListView column doesn't support different data types you can than sort by some already implemented function. You just do it yourself by implementing specific soft-algorithm for a particular column...
aristo
+1  A: 

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;
}
Hans Passant
A: 

Hi,

I think we have a column of type integer like what we have the columns in datagrid, actually i do not have idea about listview control. Let me know if you find the answer.

ListView is different from data grid view in this respect, you can have only strings. So you need to use the tag property (I have just accepted the correct answer)
Grzenio