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())

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

Appreciate any help. Thanks much!

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

Hi CodeWord/Vegasvidiot, thanks for replying!

Re: this:

VB.NET Syntax (Toggle Plain Text)

For Each row As DataGridViewRow In DataGridView1.Rows

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)


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

Thanks much!

This article has been dead for over six months. Start a new discussion instead.