Referring to this thread, I am trying to do something similar. Here is a line of my code...

ListView1.Items.Add(New ListViewItem({mydatarow("FirstName"), mydatarow("LastName")}))

VS Intellisense marks this code with the following error:

Value of type 1-dimensional array cannot be converted to System.Web.UI.WebControls.ListViewItemType

Any idea what I'm doing wrong?

Recommended Answers

All 12 Replies

Did you Dim mydatarow As DataRow() but not DataRow? Ok, normally people do that. But, you cannot add a whole array instead of an item into ListViewItem. Please use For loop to add each ListViewItem.

        For i As Integer = 0 To mydatarow.Length - 1
            ListView1.Items.Add(New ListViewItem(mydatarow(i)("FirstName").ToString, mydatarow(i)("LastName").ToString))
        Next

Using the curly braces indicates an array, but the Add method only accepts 1 item. I assume the mydatarow("LastName") is a subitem. Something like this should work:

ListView1.Items.Add(mydatarow("FirstName").ToString).SubItems.Add(mydatarow("LastName").ToString)

This is assuming of course that you've added atleast 2 columns to your listview.

Hi,

I think you can try this:

 ' Display items in the ListView control
For Each row As DataRow In dtable.Rows
    listView1.Items.Add(New ListViewItem(New String() {row("Firstname").ToString(), row("Lastname").ToString()}))
Next

Using the curly braces indicates an array, but the Add method only accepts 1 item.

The array is the parameter provided to the ListViewItem constructor which is perfectly valid. The new ListViewItem is the single parameter provided to the ListView Add method which is also valid.

Thank you your responses. The thread that I referenced in my original post recommended doing it that way (as an array), so I had no reason not to think it would for me. I will try some of your suggestions and post back.

tinstaafl,

ListView1.Items.Add(mydatarow("FirstName").ToString).SubItems.Add(mydatarow("LastName").ToString)

Trying it your way intellisense marks "mydatarow("FirstName").ToString" with the error:

Value of type string cannot be converted to System.Web.UI.WebControls.ListViewDataItem

Luc001,

listView1.Items.Add(New ListViewItem(New String() {row("Firstname").ToString(), row("Lastname").ToString()}))

I get the original error with your method:

Value of type 1-dimensional array cannot be converted to System.Web.UI.WebControls.ListViewItemType

Hi,

You try Here to find more explanation how to solve your problem.

Reverend Jim,

The array is the parameter provided to the ListViewItem constructor which is perfectly valid. The new ListViewItem is the single parameter provided to the ListView Add method which is also valid.

Here is the original section of code in question. Is there something else I'm doing or not doing that would cause intellisense to complain?

Dim dr As DataRow
Dim ds As DataSet
Dim dt As DataTable
ds = TODB.GetLogData(logid, programid, sportid, teamid, athleteid)
If ds.Tables.Count > 0 Then
    dt = ds.Tables(0)
    'lstLogData.DataSource = dt
    'lstLogData.DataBind()
    lstLogData.Items.Clear()
    For Each dr In dt.Rows
        lstLogData.Items.Add(New ListViewItem({dr("FirstName"), dr("LastName")}))
    Next
End If

Loc001,

Thanks. I had run across that article in my earlier searches. It has some good info and I have it bookmarked, but it didn't help me with my specific problem...trying to populate the ListView programmatically.

Try copying dr("FirstName") and dr("LastName") into local string variables FirstName and LastName, then try

lstLogData.Items.Add(New ListViewItem({FirstName, LastName}))

and tell me if that works.

In doing more research, it appears that in the webcontrol version of the listview it's tied more closely to using a database and takes listviewdataitems as items. The listviewdataitem constructor takes dataitemindex and displayindex as parameters.

Something like this might be closer to what you need:

    Dim dr As DataRow
    Dim ds As DataSet
    Dim dt As DataTable
    ds = TODB.GetLogData(logid, programid, sportid, teamid, athleteid)
    If ds.Tables.Count > 0 Then
        dt = ds.Tables(0)
        'lstLogData.DataSource = dt
        'lstLogData.DataBind()
        lstLogData.Items.Clear()
        For Each dr In dt.Rows
            lstLogData.Items.Add(New ListViewDataItem(1, 1))
            lstLogData.Items.Add(New ListViewDataItem(2, 2))
        Next
    End If

Not sure which actual indexes you need, but this shouold give you an idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.