[RESOLVED] [2005] problem in adding item to ListView
hi
I want to add item to ListView1 with below code:
Code:
Dim Row As Integer = 1
Dim MyItem = ListView1.Items.Add(Row)
With MyItem
.SubItems.Add("Second Item")
End With
but i get error "Item cannot be added to ListView." in "Dim MyItem = ListView1.Items.Add(Row)"
I use this code in other place of project (other forms) without any error.
wait for your idea :wave:
Re: [2005] problem in adding item to ListView
Your code works fine if I do as below. This creates two columns called "A" and "B" with "1" and "Second Item" in the first row. Is this what you intended?
Code:
ListView1.View = View.Details
ListView1.Columns.Add("A")
ListView1.Columns.Add("B")
Dim Row As Integer = 1
Dim MyItem = ListView1.Items.Add(Row)
With MyItem
.SubItems.Add("Second Item")
End With
Re: [2005] problem in adding item to ListView
Bulldog thanks for your reply but don't resolve my problem with this code
I make Columns with Edit Columns in design mode and set properties of View to Details, too
other idea :o
Re: [2005] problem in adding item to ListView
It semms you are transfering from vb6 to .Net. You need to set the "Option explicit and option strict" properties ON in your VS. Or you need to have these two lines on top of your code
vb Code:
Option Explicit On
Option Strict On
copy them as it is shown than you will see why it doesn't work.
vb Code:
Option Explicit On
Option Strict On
Public Class Form1
End Class
Re: [2005] problem in adding item to ListView
Quote:
It semms you are transfering from vb6 to .Net.
no no :ehh:
Quote:
copy them as it is shown than you will see why it doesn't work.
I have this code in my form code
other help
1 Attachment(s)
Re: [2005] problem in adding item to ListView
If you have the "Option Explicit On" and "Option Strict On" on top of your class then the parts that are not correct in your code should be underlined. If you move the mouse on top of them then it will tell you what is wrong.
Re: [2005] problem in adding item to ListView
your code should look like this:
vb Code:
Dim MyItem As New ListViewItem("I am new")
MyItem = ListView1.Items.Add(MyItem)
With MyItem
.SubItems.Add("Second Item")
End With
Re: [2005] problem in adding item to ListView
Quote:
your code should look like this:
vb Code:
Dim MyItem As New ListViewItem("I am new")
MyItem = ListView1.Items.Add(MyItem)
With MyItem
.SubItems.Add("Second Item")
End With
I useing this code but i get error again
error: "Item cannot be added to ListView." in "MyItem = ListView1.Items.Add(MyItem)"
Re: [2005] problem in adding item to ListView
2 Attachment(s)
Re: [2005] problem in adding item to ListView
yes. I attached My Code
I attached 2 code. AllD.vb work very well but ProSearch.vb have an error
Re: [2005] problem in adding item to ListView
It would be better if you just post the section of the code because some people have issues with downloading staff.
Re: [2005] problem in adding item to ListView
Code:
If CheckBox1.Checked = True Then
Select Case SeaType
Case "allname"
TSQL = "SELECT * FROM hd WHERE fname LIKE '" & LLike & TextBox3.Text & RLike & "' AND lname LIKE '" & LLike & TextBox2.Text & RLike & "'"
Case "status"
If RadioButton5.Checked = True Then TSQL = "SELECT * FROM hd WHERE status ='inact'"
If RadioButton6.Checked = True Then TSQL = "SELECT * FROM hd WHERE status ='act'"
Case "cost"
TSQL = "SELECT * FROM hd WHERE cost " & Opera & " '" & TextBox5.Text & "'"
Case "allcost"
TSQL = "SELECT * FROM hd WHERE allcost " & Opera & " '" & TextBox5.Text & "'"
Case "fname"
TSQL = "SELECT * FROM hd WHERE fname LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "lname"
TSQL = "SELECT * FROM hd WHERE lname LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "uname"
TSQL = "SELECT * FROM hd WHERE uname LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "coname"
TSQL = "SELECT * FROM hd WHERE coname LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "tel"
TSQL = "SELECT * FROM hd WHERE tel LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "mob"
TSQL = "SELECT * FROM hd WHERE mob LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "address"
TSQL = "SELECT * FROM hd WHERE address LIKE '" & LLike & TextBox4.Text & RLike & "'"
Case "email"
TSQL = "SELECT * FROM hd WHERE email LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "domain"
TSQL = "SELECT * FROM hd WHERE [domain] LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "host"
TSQL = "SELECT * FROM hd WHERE host LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "panel"
TSQL = "SELECT * FROM hd WHERE panel LIKE '" & LLike & TextBox1.Text & RLike & "'"
Case "info"
TSQL = "SELECT * FROM hd WHERE info LIKE '" & LLike & TextBox4.Text & RLike & "'"
Case "khost"
TSQL = "SELECT * FROM hd WHERE khost LIKE '" & LLike & ComboBox2.Text & RLike & "'"
Case "time"
TSQL = "SELECT * FROM hd WHERE time LIKE '" & LLike & Numeric.Value & RLike & "' AND KDuration='" & Duration & "'"
Case "bdate"
TSQL = "SELECT * FROM hd WHERE bdate LIKE '" & LLike & MTextBox1.Text & RLike & "'"
Case "edate"
TSQL = "SELECT * FROM hd WHERE edate LIKE '" & LLike & MTextBox1.Text & RLike & "'"
End Select
con.Open()
cmd.CommandText = TSQL
cmd.Connection = con
data = cmd.ExecuteReader
Do While data.Read
If data("Panel") <> "nothing" Then
Pnl = data.Item("Panel")
Else
Pnl = "-----"
End If
If data("status") = "act" Then
Sta = "فعال"
ElseIf data("status") = "inact" Then
Sta = "غيرفعال"
End If
Dim MyItem As New ListViewItem
Name = data("Fname") & " " & data("Lname")
MyItem = ListView1.Items.Add(Row)
With MyItem
.SubItems.Add("هاست و دامين")
.SubItems.Add(data("uname"))
.SubItems.Add(Name)
.SubItems.Add(data("Coname"))
.SubItems.Add(data("tel"))
.SubItems.Add(data("Mob"))
.SubItems.Add(data("Email"))
.SubItems.Add(data("address"))
.SubItems.Add(data("domain"))
.SubItems.Add(data("host"))
.SubItems.Add(data("BandW"))
.SubItems.Add(data("khost"))
.SubItems.Add(Pnl)
.SubItems.Add(data("cost"))
.SubItems.Add(data("allcost"))
.SubItems.Add(data("bdate"))
.SubItems.Add(data("time") & " " & ComboBox4.Text)
.SubItems.Add(data("edate"))
.SubItems.Add(Sta)
.SubItems.Add(data("info"))
End With
Row += 1
Loop
con.Close()
End If
is piece of my code
some of string is persian :p
1 Attachment(s)
Re: [2005] problem in adding item to ListView
Shamsoft, I thought we went through this but it seems you didn’t understand it. What is the “Row”? It is an integer! Does “ListView1.Items.Add” method except integer type parameter? No!
So why and how come you have it there? You said that you have the “Option Strict” set to On so how come you don’t see that it is underlined?
Re: [2005] problem in adding item to ListView
you need to convert the "Row" integer to string
vb Code:
MyItem = ListView1.Items.Add(CStr(Row))
Re: [2005] problem in adding item to ListView
Quote:
What is the “Row”? It is an integer! Does “ListView1.Items.Add” method except integer type parameter? No!
yeah. is integer. it mean i cannot using integer value in this parameter?
Quote:
You said that you have the “Option Strict” set to On so how come you don’t see that it is underlined?
no. I don't use Option Explicit On & Option Strict On. because i don't use this Option for my codes but they have not any error message. for example i don't use Option Explicit On & Option Strict On in AllD.vb and also using ListView. but it have not any error
Re: [2005] problem in adding item to ListView
Quote:
Originally Posted by shamsoft
yeah. is integer. it mean i cannot using integer value in this parameter?
No you cant. You need to take a look what types the method excepts as a parameter.
Quote:
no. I don't use Option Explicit On & Option Strict On. because i don't use this Option for my codes but they have not any error message. for example i don't use Option Explicit On & Option Strict On in AllD.vb and also using ListView. but it have not any error
I don't know why you don't use Option Explicit On & Option Strict On in your projects but it is a good way of notifing about the errors. My guess is that you don't use it becuse you have non latin letters in your code. But you still can use it and ignore some errors while you are coding. And when you want to run the project than just comment them out.
Re: [2005] problem in adding item to ListView
Quote:
No you cant. You need to take a look what types the method excepts as a parameter.
ok. thanks a lot. My problem Resolved :thumb:
Quote:
I don't know why you don't use Option Explicit On & Option Strict On in your projects but it is a good way of notifing about the errors. My guess is that you don't use it becuse you have non latin letters in your code. But you still can use it and ignore some errors while you are coding. And when you want to run the project than just comment them out.
yeah. i must be use the Option Explicit On & Option Strict On in my project. because i programming by vb6 in last (and now) and i beginner in vb.net, i don't habit to use Option Explicit On & Option Strict On in my project. but i try to using this.
thanks to your helps again ;)
Re: [2005] problem in adding item to ListView
Just a tip, you can set these options for your Visual Studion so that you don't need to have these lines on top of each class. To set it in vs you need to do:
Click on Tools menu button and select "Options" menu item. Expend the "Projects and Solution" node and select "VB Defaults" and set the options to On at the right.
Re: [2005] problem in adding item to ListView