Hi,

I want to populate a listview with items from datagrid view. I've been searching for solutions but it seems that all the scenarios I found were about populating listview with items from tables in database, not from another listview or datagrid view.

I'm trying this code:

For count As Integer = 0 To (DataGridView1.Rows.Count() - 1)
                    Dim row As DataRow = DataGridView1.Rows(count)
                    Dim item As ListViewItem = New ListViewItem(row(fieldfirstcol).ToString())
                    item.SubItems.Add(row(fieldsecondcol.ToString()))
                    ListView1.Items.Add(item)

but it doesn't work; it says DataGridViewRow cannot be converted to DataRow.

Appreciate any help. Thanks much!

Recommended Answers

That's because the datagridview.rows collection contains DataGridViewRow objects, not DataRow Objects. This is because the Datagridview can display many kinds of data and not just datarows from a datatable.

If you want to iterate through them and peel off data you can do something like this:


       
Jump to Post

All 3 Replies

Try This code

[Dim C As Integer]
[ Dim DV As New DataView]
[ DV = DataGrid1.DataSource]
[ For C = 0 To DV.Count]
[ ListView1.Items.Add(CType(DV(C)("FieldName"), String).Trim)]
[ Next]

That's because the datagridview.rows collection contains DataGridViewRow objects, not DataRow Objects. This is because the Datagridview can display many kinds of data and not just datarows from a datatable.

If you want to iterate through them and peel off data you can do something like this:

For Each row As DataGridViewRow In DataGridView1.Rows
            Listview1.Items.Add(row.Cells("ColumnName").Value)
        Next

Hi CodeWord/Vegasvidiot, thanks for replying!

Re: this:

VB.NET Syntax (Toggle Plain Text)

1.
For Each row As DataGridViewRow In DataGridView1.Rows
2.
Listview1.Items.Add(row.Cells("ColumnName").Value)
3.
Next

I tried it. The error says "Overload resolution failed because no Public 'Add' is most specific for these arguments: 'Public Overrides Function Add(text As String) As System.Windows.Forms.ListViewItem': Not most specific. 'Public Overrides Function Add(value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem': Not most specific." I don't understand what this means :=) I tried to use an index too but I got the same error.

Also how about if the listview has multiple columns? Shouldn't I use a SubItem? I can't have a subitem property directly from the listview.

I tried this:

For Each row As DataGridViewRow In DataGridViewCart.Rows
            Dim rowlist As DataRow
            Dim item As ListViewItem = New ListViewItem(rowlist(row.Cells("Number").Value).ToString)
            item.SubItems.Add(rowlist(row.Cells("AmountDue").Value).ToString)
            FormPayment.listduePOs.Items.Add(item)

        Next

But it still doesn't work. Sigh :=(

Thanks much!

Be a part of the DaniWeb community

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