954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pass values from ListView to Datagridview

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

haqayyum
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

haqayyum
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

See if this link helps.

codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

LearnVBnet
Light Poster
40 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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, ""}
Invoice.DGV.Rows.Add(lvItem)
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.

LearnVBnet
Light Poster
40 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Check out this link .

codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: