hi daniweb members

I am using visual basic 2008. i have a listview and a datagridview which have both 3 columns like ProductCode,ProductName & ProductPrice. Now i want to pass listview values to datagridview in the same sequence. I want that when i double click on any row in the listview these row values inserted to Datagirdview. please tell me how does it.

thanx daniweb in advance

Recommended Answers

All 6 Replies

See if this helps.

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                Dim lvItem() As String = {.Text, .SubItems(1).Text, .SubItems(2).Text} '// get ListView selectedItem.
                DataGridView1.Rows.Add(lvItem) '// add it to DataGridView.
            End With
        End If
    End Sub

Thank you very much I applied your code after little changing in it but the following erorr msg are swhon at runtime
my coding is given below

If Not ProductLV.SelectedItems.Count = 0 Then '// check if item is selected.
            With ProductLV.SelectedItems(0)
                Dim lvItem() As String = {.Text, .SubItems(1).Text, .SubItems(2).Text} '// get ListView selectedItem.
                Quotationfrm.DataGridViewX1.Rows.Add(lvItem) '// add it to DataGridView.
            End With

but showing following error msg
"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

See if this helps.

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                Dim lvItem() As String = {.Text, .SubItems(1).Text, .SubItems(2).Text} '// get ListView selectedItem.
                DataGridView1.Rows.Add(lvItem) '// add it to DataGridView.
            End With
        End If
    End Sub

This code is ok If State = modProcedure.FormState.adStateAddMode
But how can we add selecteditem
if our datagridview currentcell > 0
with state = modProcedure.FormState.adStateEditMode

'===========

If State = modProcedure.FormState.adStateAddMode Then
DGV.Columns.Add("ProductCode", "Product Name")
DGV.Columns.Add("Quantity", "Quantity")
DGV.Columns.Add("UnitPrice", "Price")
DGV.Columns.Add("Extention", "Total")

ElseIf State = modProcedure.FormState.adStateEditMode Then
Try
Dim qryInvoice As String = "SELECT ProductCode,Quantity,UnitPrice,Extention, " _
+ " FROM InvoiceDetail where InvNomer ='" & pbInvNomer & "' "
Call ConnecString()
Dim daInvoice As New OleDbDataAdapter()
Dim dsInvoice As New DataSet()
daInvoice.SelectCommand = New OleDbCommand(qryInvoice, mConn)
daInvoice.Fill(dsInvoice, "InvoiceDetail")
dsInvoice.Tables("InvoiceDetail").DefaultView.AllowNew = True
DGV.DataSource = dsInvoice.Tables("InvoiceDetail")

Me.DGV.Columns(0).HeaderText = "Product Name"
Me.DGV.Columns(1).HeaderText = "Quantity"
Me.DGV.Columns(2).HeaderText = "Price"
Me.DGV.Columns(3).HeaderText = "Total"

'==========

1. If State = modProcedure.FormState.adStateAddMode -----> can Add to datagridview
2. if State = modProcedure.FormState.adStateEditMode ----> cannot Add to datgridview

If Not ListProduct.SelectedItems.Count = 0 Then
            For Each item As ListViewItem In ListProduct.SelectedItems
                With ListProduct.SelectedItems(0)
                    Dim lvItem() As String = {.Text, "", .SubItems(2).Text, "", Convert.ToString(warekey), "", .SubItems(5).Text, ""}
                    [COLOR="Red"]Invoice.DGV.Rows.Add(lvItem)[/COLOR]
                End With
            Next
        End If
        Me.Hide()
    End Sub

InvalidOperationException
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

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.