0

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

3
Contributors
6
Replies
21
Views
6 Years
Discussion Span
Last Post by codeorder
0

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
0

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."

Edited by Dani: Formatting fixed

0

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

Edited by LearnVBnet: n/a

0
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.

Edited by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.