Is this correct (any other way)...

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

I have a detailed view listbox with 4 colums.
I have found out a way to add rows, but is
this the correct way of doing this?
Where do I put a uniqe value to a row.
(listViewItem.Tag = UniqeRowID)
This is NOT the index number, but my own ID.

string[] s=new string[10]; // extra spare
s[0] = "Data Col 1";
s[1] = "Data Col 2";
s[2] = "Data Col 3";
s[3] = "Data Col 4";
System.Windows.Forms.ListViewItem listViewItem = new ListViewItem(s);
listViewItem.Tag = UniqeRowID;
this.listView1.Items.AddRange(new ListViewItem[] {listViewItem} );

And how do I get the uniqe identifier back again?
(I only need the first available selected row)

ListView.SelectedListViewItemCollection selItems =
this.listView1.SelectedItems;
foreach ( ListViewItem item in selItems)
{
UniqeRowID = Convert.ToInt32(item.Tag.ToString());
}
 
Hi GTI,

The Tag property stores all information as object references, so when you feed it an int you need to cast it back to int before you use it.

int i = (int)item.Tag;

As for adding new rows. You could add the data the way you do, but in your case you might prefer

ListViewItem lvi = new ListViewItem("column1");
lvi.SubItems.Add("column2");
lvi.SubItems.Add("column3");
lvi.SubItems.Add("column4");
lvi.Tag = ID;

listView1.Items.Add(lvi);



I have a detailed view listbox with 4 colums.
I have found out a way to add rows, but is
this the correct way of doing this?
Where do I put a uniqe value to a row.
(listViewItem.Tag = UniqeRowID)
This is NOT the index number, but my own ID.

string[] s=new string[10]; // extra spare
s[0] = "Data Col 1";
s[1] = "Data Col 2";
s[2] = "Data Col 3";
s[3] = "Data Col 4";
System.Windows.Forms.ListViewItem listViewItem = new ListViewItem(s);
listViewItem.Tag = UniqeRowID;
this.listView1.Items.AddRange(new ListViewItem[] {listViewItem} );

And how do I get the uniqe identifier back again?
(I only need the first available selected row)

ListView.SelectedListViewItemCollection selItems =
this.listView1.SelectedItems;
foreach ( ListViewItem item in selItems)
{
UniqeRowID = Convert.ToInt32(item.Tag.ToString());
}
 
Hi Morten,
Tanks for the feedback.
As a newbie it is always nice to get feedback of my practise.

GTi

Morten Wennevik said:
Hi GTI,

The Tag property stores all information as object references, so when you
feed it an int you need to cast it back to int before you use it.

int i = (int)item.Tag;

As for adding new rows. You could add the data the way you do, but in
your case you might prefer

ListViewItem lvi = new ListViewItem("column1");
lvi.SubItems.Add("column2");
lvi.SubItems.Add("column3");
lvi.SubItems.Add("column4");
lvi.Tag = ID;

listView1.Items.Add(lvi);



I have a detailed view listbox with 4 colums.
I have found out a way to add rows, but is
this the correct way of doing this?
Where do I put a uniqe value to a row.
(listViewItem.Tag = UniqeRowID)
This is NOT the index number, but my own ID.

string[] s=new string[10]; // extra spare
s[0] = "Data Col 1";
s[1] = "Data Col 2";
s[2] = "Data Col 3";
s[3] = "Data Col 4";
System.Windows.Forms.ListViewItem listViewItem = new ListViewItem(s);
listViewItem.Tag = UniqeRowID;
this.listView1.Items.AddRange(new ListViewItem[] {listViewItem} );

And how do I get the uniqe identifier back again?
(I only need the first available selected row)

ListView.SelectedListViewItemCollection selItems =
this.listView1.SelectedItems;
foreach ( ListViewItem item in selItems)
{
UniqeRowID = Convert.ToInt32(item.Tag.ToString());
}
 
Back
Top