Problems adding items to a listview

  • Thread starter Thread starter Eva
  • Start date Start date
E

Eva

Hi,

I wanted to know how i can enter values into a specific
column of a listview. I have tried the following code but
this seems to enter all my values into the first column!!!

Can anyone please help me out on this??

hers my code so far.....

Private Sub btnSelRoom_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSelRoom.Click

Dim ChosenDate As Date
ChosenDate = dtpRoomDate.Value.ToShortDateString
Dim ChosenRoom As SqlString
ChosenRoom = CBoxRoomNames.SelectedItem
Dim ChosenLayout As SqlString
ChosenLayout = CBoxLayout.SelectedItem
Dim ChosenSession As SqlString
ChosenSession = CBoxSession.SelectedItem
Dim ChosenGuestNo As String
ChosenGuestNo = txtNoOfGuests.Text
Dim ChosenMeal As String

Dim FullDescription As String
FullDescription = String.Format("{0}, {1}, {2}, {3}, {4}",
ChosenDate, ChosenRoom, ChosenLayout, ChosenSession,
ChosenGuestNo)

LViewSelRooms.Items.Add(FullDescription)

End Sub



Many thx.
And Merry xmas to all :o)
 
Hi Cor,

thx for the link, it was very useful. im still however
having problems. im trying to insert a value obtained
from a combobox into the subitem of the listview but i
keep getting the following error....

Overload resolution failed because no accessible 'Add'
can be called with these arguments:
'Public Overloads Function Add(text As String) As
System.Windows.Forms.ListViewItem.ListViewSubItem': Value
of type 'System.Data.SqlTypes.SqlString' cannot be
converted to 'String'.
'Public Overloads Function Add(item As
System.Windows.Forms.ListViewItem.ListViewSubItem) As
System.Windows.Forms.ListViewItem.ListViewSubItem': Value
of type 'System.Data.SqlTypes.SqlString' cannot be
converted
to 'System.Windows.Forms.ListViewItem.ListViewSubItem'.


I have no idea what im doin wrong!! the code i have has
been pasted below. Can u spot my Mistake??.....

Private Sub btnSelRoom_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSelRoom.Click
Dim ChosenDate As Date
ChosenDate = dtpRoomDate.Value.ToShortDateString
Dim ChosenRoom As SqlString
ChosenRoom = CBoxRoomNames.SelectedItem
Dim ChosenLayout As SqlString
ChosenLayout = CBoxLayout.SelectedItem
Dim ChosenSession As SqlString
ChosenSession = CBoxSession.SelectedItem
Dim ChosenGuestNo As String
ChosenGuestNo = txtNoOfGuests.Text

Dim Item1 As New ListViewItem(ChosenDate, 0)
Item1.SubItems.Add(ChosenRoom)
Item1.SubItems.Add("ChosenLayout")

LViewSelRooms.Items.AddRange(New ListViewItem() {Item1})

Many thx
 
Hi Eva,

It is always a gamble but I think this can it be
Dim Item1 As New ListViewItem(ChosenDate, 0)
Item1.SubItems.Add(ChosenRoom)
Item1.SubItems.Add("ChosenLayout")
LViewSelRooms.Items.AddRange(New ListViewItem() {Item1})

Dim Item1 As New ListViewItem(ChosenDate, 0)
Item1.SubItems.Add(ChosenRoom)
Item1.SubItems.Add("ChosenLayout")
LViewSelRooms.Items.Add(Item1)

I hope this is the sollution.
(Cannot be that big beside it if it is not).

Cor
 
Hi Cor,

I made the ammendment and im still getting the same
error!!
The problem seems to occur when i remove the quote marks
("") from around the item that is being added.

i.e if i add the following code...
Item1.SubItems.Add("ChosenRoom")
it works fine but it only adds the value ChosenRoom into
my listview.

I want to add the value that is contained in this
varibale to be added in the listview so i tried...
Item1.SubItems.Add(ChosenRoom)
this however produces the error...

Overload resolution failed because no accessible 'Add'
can be called with these arguments:
'Public Overloads Function Add(text As String) As
System.Windows.Forms.ListViewItem.ListViewSubItem': Value
of type 'System.Data.SqlTypes.SqlString' cannot be
converted to 'String'.
'Public Overloads Function Add(item As
System.Windows.Forms.ListViewItem.ListViewSubItem) As
System.Windows.Forms.ListViewItem.ListViewSubItem': Value
of type 'System.Data.SqlTypes.SqlString' cannot be
converted
to 'System.Windows.Forms.ListViewItem.ListViewSubItem'.


how do i actually add the value contained in the variable
into the listview?

thx
 
Hi,

What i just found out is that this may be a datatype
problem. It doesnt seem to like the sqlstring datatype
that i am using. I tried changing this to just STRING i.e.
.....
Dim ChosenLayout As String
ChosenLayout = CBoxLayout.SelectedItem

but i get a message stating...
An unhandled exception of
type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll
Additional information: Cast from type 'SqlString' to
type 'String' is not valid.

what can i do to correct this?
 
Hi Eva,

The first thing you should to put the Option Strict On in top of your
program.
(It does not help but shows you this kind of errors in develope time).

Often something simple as

a = b.tostring does than help.

But try first this.

(I do not direct understand on that place that cast error from string to
string but try this first please)

Cor
 
Typoes correction
The first thing you should to put the Option Strict On in top of your
program.
(It does not help but shows you this kind of errors in develope time).
The first thing you should do, is put the Option Strict On in top of your
program.

It does not help, but it shows you this kind of errors in development time).
 
You might find it useful to inherit from a listViewItem
and add your object as a custom property.
eg.

Private Class MyListViewItem
Inherits Windows.Forms.ListViewItem

Private m_obj As Object

Friend Sub New(ByVal Text As String, ByVal SomeObject
as Object)
Me.m_obj = SomeObject
MyBase.New(Text)
With MyBase.SubItems
.Add(m_obj.SomeProperty)
.Add(m_obj.SomeOtherProperty)
End With
MyBase.UseItemStyleForSubItems = False
End Sub

Friend ReadOnly Property SomeObject() As Object
Get
Return Me.m_obj
End Get
End Property
End Class

Dim obj1 as New object
obj1.SomeProperty = "Ben"
obj1.SomeOtherProperty = "Reese"
'you can then add you inherited object to the list view
ListView1.Add(New MyListViewItem("HI", obj1))

Dim obj2 as object
obj2 = Ctype(ListView1.Items(0),MyListViewItem).SomeObject

msgBox(String.Format("Hello {0}
{1}",obj2.SomeProperty,obj2.SomeOtherProperty))

I have found this approach to be extremely usefull in a
number of situations.

HTH
Ben
 
you do have a data type problem

Lisview items and sub items take strings as parameters not
sqlString.

Consider using the ToString method on the SqlString

Dim s as New System.Data.SQLTypes.SQLString("SELECT * FROM
Products")

Listview.Items.Add("Hi")
ListView.Items(0).SubItems.Add(s.ToString)

Alternatively see my earlier post which has accidently
gone in higher up the tree on inheriting from the
listviewitem.

Ben
 
Back
Top