Results 1 to 11 of 11

Thread: [RESOLVED] Getting Data from listView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Resolved [RESOLVED] Getting Data from listView

    Hi i currently have a listview on my form that is set to "details" view and is popoulated by a table in my sql database.

    what i want to be able to do is when i double click on a selected row i want all the details in that row to appear in another form so that i can edit them.

    how do i populate the edit form with the data from the selected row i am double clicking on?

    can anyone help me?

    Thanks Roofuss

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting Data from listView

    is your listview multiselect?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Getting Data from listView

    No its note not multi select just full row select

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting Data from listView

    if not multiselect, try this:

    vb Code:
    1. Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    2.     MsgBox(ListView1.Items(ListView1.SelectedIndices(0)).SubItems(1).Text) 'gets text from second column
    3. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Getting Data from listView

    no sorry that doesnt work and it only brings the 1st column up in a message box i need to be able to display the data in my edit form which is called frmCreateTask

    the code i have at the moment is as follows:
    vb Code:
    1. Private Sub lvTasks_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvTasks.DoubleClick
    2.  
    3.         Dim Row As DataRow = lvTasks.Items(lvTasks.SelectedItems).Row
    4.         Using dialogue As New frmCreateTask(Row)
    5.             dialogue.ShowDialog()
    6.         End Using
    7.     End Sub

    but causes an overload resolution

    any ideas?

    thanks

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting Data from listView

    ok. try this:

    vb Code:
    1. Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    2.     Dim lvi As ListViewItem = ListView1.Items(ListView1.SelectedIndices(0))
    3.     Dim frm As New frmCreateTask(lvi)
    4.     frm.Show()
    5. End Sub

    frmCreateTask should have a sufficient number of textboxes, named Textbox1, Textbox2, etc:

    vb Code:
    1. Public Class frmCreateTask
    2.  
    3.     Public Sub New(ByVal lvi As ListViewItem)
    4.         InitializeComponent()
    5.         For x As Integer = 1 To lvi.SubItems.Count
    6.             Me.Controls("textbox" & x.ToString).Text = lvi.SubItems(x - 1).Text
    7.         Next
    8.     End Sub
    9.  
    10. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Getting Data from listView

    i think we're getting there but now i get a run time error that says

    "Object reference not set to an instance of an object." at the highlighted area

    [HEIGHLIGHT] Public Sub New(ByVal lvi As ListViewItem)
    InitializeComponent()
    For x As Integer = 1 To lvi.SubItems.Count
    Me.Controls("textbox" & x.ToString).Text = lvi.SubItems(x - 1).Text
    Next
    End Sub

    [/HEIGHLIGHT]

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting Data from listView

    how many subitems are there? do you have that many textboxes on the form (frmCreateTask)? are they named textbox1, textbox2, etc?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Getting Data from listView

    I have got 6 sub items in total and on frmCreateTask i have 6 textboxes and one drop down box. they are all labelled with reference to what they to but to make it easier and faster we can just use textbox1, textbox2 etc.. if you like?

    Thanks

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting Data from listView

    you could just type them out instead of using a loop:

    txtWhatever.text = lvi.subitems(0).text
    'etc

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Getting Data from listView

    At 6:00 in the morning you are offically my hero. thats worked great !!!

    thanks for all your help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width