Click here to Skip to main content
14,774,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim lvt As ListViewItem
        If dataGrid2.SelectedRows.Count >= 0 Then

            For Each row As DataGridViewRow In dataGrid2.SelectedRows
                lvt = New ListViewItem()
                lvt.SubItems.Add(row.Cells(1).Value.ToString())
                lvt.SubItems.Add(row.Cells(2).Value.ToString())
                bookList.Items.Add(lvt)
            Next

        Else
            MsgBox("Select a Book!", MsgBoxStyle.Exclamation, "Warning")
        End If


    End Sub
Posted
Comments
Michael Portanoba 17-Oct-12 0:56am
   
my code wont work.. after i press Add button, nothing happens

For Each row As DataGridViewRow In dataGrid2.SelectedRows
  bookList.Items.Add(row.Cells(1).Value.ToString(),row.Cells(2).Value.ToString(),0)
Next

Happy Coding!
:)
   
v3
Comments
Michael Portanoba 17-Oct-12 1:29am
   
thank you for the answer. but i cant seem to understand that it has blue underline on the code bookList.Items.Add(row.Cells(1).Value.ToString(),row.Cells(2).Value.ToString(),Nothing)
Aarti Meswania 17-Oct-12 1:30am
   
please see updated solution
Michael Portanoba 17-Oct-12 1:33am
   
yes it has an error and this is what it says. ( Overload resolution failed because no accessible 'Add' is most specific for these arguments:
'Public Overridable Function Add(key As String, text As String, imageIndex As Integer) As System.Windows.Forms.ListViewItem': Not most specific.)
'Public Overridable Function Add(key As String, text As String, imageKey As String) As System.Windows.Forms.ListViewItem': Not most specific.
Aarti Meswania 17-Oct-12 1:34am
   
please see updated solution write
0 instead of Nothing
Michael Portanoba 17-Oct-12 1:38am
   
alright!! thanks Aarti Meswania it works fine now! :)
Aarti Meswania 17-Oct-12 1:39am
   
welcome!
Glad to help you! :)
Michael Portanoba 17-Oct-12 21:53pm
   
hello.. its me again.. just want to ask that when i select the row in data grid view, then click add, in the list view it only shows only one cell. please help. :) thanks in advanced
Aarti Meswania 18-Oct-12 0:38am
   
list view have one cell with text value & 1 cell with key value (hidden) not more than 2 cells, so if you want more than 2 cells than go with gridview and change it's look
:)
Michael Portanoba 18-Oct-12 0:40am
   
so you mean that i will combine the Book title and the author? right? cause they are in a different columns
Aarti Meswania 18-Oct-12 0:45am
   
hmm
or
you can use datagrid that have look like do not show column headers & row headers , make it's cell style raised/suken... one of them will remove lines in grid
and set multi line property true (it will look like listview)
Michael Portanoba 18-Oct-12 0:49am
   
ok.. but, what my problem is, after i click the row header in the datagridview, then click add button (which is suppose to go on the ListView) it only displays one cell. it is suppose to the book title and the author. what do you think is missing on the code? :)
Aarti Meswania 18-Oct-12 0:51am
   
sorry not clear
Michael Portanoba 18-Oct-12 0:54am
   
wait ill post the senario :)
Michael Portanoba 18-Oct-12 1:00am
   
how to put image here? :D
Aarti Meswania 18-Oct-12 1:01am
   
no, it doesn't allow to post image
Michael Portanoba 18-Oct-12 1:03am
   
oh ok.. have you seen the codes ive posted? i try to put them in an individual lines so the book title and author would come up on the list view
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click



        If dataGrid2.SelectedCells.Count >= 0 Then

            For Each row As DataGridViewRow In dataGrid2.SelectedRows

                bookList.Items.Add(row.Cells(1).Value.ToString())
                bookList.Items.Add(row.Cells(2).Value.ToString())

            Next

        Else

            MsgBox("Select a Book!", MsgBoxStyle.Exclamation, "Warning")

        End If
   
Comments
Aarti Meswania 18-Oct-12 1:07am
   
try this
bookList.Items.Add(row.Cells(1).Value.ToString() & " (" & row.Cells(2).Value.ToString() & ")")
I have only this idea show author name in brackets
Michael Portanoba 18-Oct-12 1:11am
   
Alright!! thanks.. :D very much.. it worked fine..
Aarti Meswania 18-Oct-12 1:13am
   
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900