ListViewItem question

N

naikrovek

Hello all.

I have a ListView on a form, that has columns added at design-time but
will be populated run-time. So, I know what the columns will be, and I
know that I will have an arbitrary number of 'rows' in the ListView,
anywhere from 1 to 100 or even 1000.

The purpose of the program is to allow a user to keep an eye on tickets
in a ticketing database.

The user selects a ticket type from a drop down, and enters a ticket
number in a text box, then clicks 'add'. This adds those strings to
the first two columns of the ListView, and I want to be able to fill in
the rest of the columns of that 'row' programmatically based on data
from a database.

How do I access each element in the 'rows'? I use "rows" because I
don't know of a more correct term.

All I've gotten so far is this:

foreach (ListViewItem lvi in lvRecords.Items) {
}

I've tried accessing lvi.SubItems but I'm not at all sure how to make
it work.

Can anyone share information on how to access individual cells in the
ListView programmatically?
 
G

Guest

Here is a code snippet I had handy that illustrates getting at the subItems
of each ListViewItem:

private void btnDone_Click(object sender, EventArgs e)
{
if (this.listView1.Items.Count == 0)
{
this.Close();
return;
}
ListViewItem myItem = null;
int checkedItems = 0;
//string sipUserId = String.Empty;
foreach (ListViewItem itm in this.listView1.Items)
{
if (itm.Checked)
{
myItem = itm;
checkedItems++;

}
}
if (checkedItems > 1)
{
this.lblMessage.Text = "Check 1 Item only.";
return;
}
else if (checkedItems == 1)
{
// myItem.SubItems[1].Text+" "
this.chosenSipUserId = myItem.SubItems[0].Text +"|"
+myItem.SubItems[2].Text;
this.OnNotifyCaller();
this.Close();
}

} // end btnDone_Click
 
N

naikrovek

that solution is so simple I've glossed over it at least 20 times
trying to figure it out.

For those of you that don't see it: SubItems[int].Text

Thank You Very Much, Peter.
 

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