ListView Question

PaulieORF

Newcomer
Joined
Oct 21, 2004
Messages
24
I would like to make my ListView behave like a ListBox, as in I want to be able to have the value of the item I click on be put into a string that I've dimmed, something to the effect of SelectedValue. But I noticed that ListView does not have this option. Thanks guys.
 
The listview has a SelectedItems property that you can use ie

Code:
YourListView.SelectedItems[0].Text

Will get the text in the first column

Code:
YourListView.SelectedItems[0].SubItems[1].Text

For Other Columns

If you allow multi select you can use the count property to loop through all selected items.
 
Okay, I had just figured that part out. But now my questin is this: How can I format a subitem column in my ListView to display currency? My column is bound to a DataSet column, which itself is in Curreny format, but when I display it in my ListView it is not in currency format. Here is my code for this column:

mItem.SubItems.Add(mRow.Item("Total"))
 
I don't believe there is any way to format a column in a listview, you can only set Text, Text alignment and Width. I use a class that takes a field and returns a string that is formatted to the way I want it.
 
Usually you'll use one of the format functions...
mItem.Subitems.Add(FormatCurrency(mRow.Item("Total")))

And you can use the same format functions on a column text.
 
Awesome, that works great! Now, my only other question is how can I align this subitem to the right so that the decimals line up? I tried using the same method as I used to align the column headers, but it doesn't seem to work for the actual items themselves. Thanks.
 
Either by setting the columns TextAlign properity to Right in Design Mode when you defined and named the column or via code also when the column is defined:

Visual Basic:
ListView1.Columns.Add("column name", 60, HorizontalAlignment.Right)
 
Back
Top