Listview problem

P

Pete Kane

Hi All, I'm having a problem retrieving subitems from a listview using the string indexer method, my code for filling the lv is as
shown below

// take it as read there is data in the dataset.
foreach (DataRow dr in this.dsArt39Benefics.Tables[0].Rows)
{
lvi = new ListViewItem(dr["BenName"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine1"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine2"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine3"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine4"].ToString());
lvi.SubItems.Add(dr["CreateDate"].ToString());
lvi.SubItems.Add(dr["CreateUser"].ToString());
lvi.SubItems.Add(dr["AmendDate"].ToString());
lvi.SubItems.Add(dr["AmendUser"].ToString());

this.lvBeneficialOwnersNameAddress.Items.Add(lvi);

}

Later in the code I try to retrieve a value like this

ListViewItem lvi = this.lvBeneficialOwnersNameAddress.SelectedItems[0];

string s = lvi.SubItems["AmendUser"].Text;


the string s is always null, however, if I do it this way (the code below) it works, anyone know why ?

ListViewItem lvi = this.lvBeneficialOwnersNameAddress.SelectedItems[0];

string s = lvi.SubItems[8].Text;
 
T

TheSteph

I think you forgot to create the columns in the ListView :

lvi.Columns.Add("Ben_AddrLine1");


Pete Kane said:
Hi All, I'm having a problem retrieving subitems from a listview using the
string indexer method, my code for filling the lv is as
shown below

// take it as read there is data in the dataset.
foreach (DataRow dr in this.dsArt39Benefics.Tables[0].Rows)
{
lvi = new ListViewItem(dr["BenName"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine1"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine2"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine3"].ToString());
lvi.SubItems.Add(dr["Ben_AddrLine4"].ToString());
lvi.SubItems.Add(dr["CreateDate"].ToString());
lvi.SubItems.Add(dr["CreateUser"].ToString());
lvi.SubItems.Add(dr["AmendDate"].ToString());
lvi.SubItems.Add(dr["AmendUser"].ToString());

this.lvBeneficialOwnersNameAddress.Items.Add(lvi);

}

Later in the code I try to retrieve a value like this

ListViewItem lvi = this.lvBeneficialOwnersNameAddress.SelectedItems[0];

string s = lvi.SubItems["AmendUser"].Text;


the string s is always null, however, if I do it this way (the code below) it works, anyone know why ?

ListViewItem lvi = this.lvBeneficialOwnersNameAddress.SelectedItems[0];

string s = lvi.SubItems[8].Text;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top