Results 1 to 4 of 4

Thread: ** RESOLVED** [2005] Copying Listview columns

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved ** RESOLVED** [2005] Copying Listview columns

    I've been searching the forum for an answer. I have found many that were close, but none that seemed to exactly handle this. I have a listview (ListView1). It has 4 columns. I want to extract the whole thing to memory so my CFO can paste it into an Excel spreadsheet. There is no need for a direct listview ==> Excel yet, only to memory. Here is what I have so far:

    VB Code:
    1. My.Computer.Clipboard.Clear()
    2.  
    3.         Dim wholeRow As String = ListView1.Text
    4.  
    5.         For count As Integer = 1 To ListView1.Items.Count - 1
    6.             wholeRow += ControlChars.Tab + ListView1.Items(count).Text
    7.             My.Computer.Clipboard.SetText(wholeRow)
    8.         Next
    9.         MsgBox("Data extracted", MsgBoxStyle.Information)

    The problem is that it only extracts column 0 ( no subitems). Also, I need to get a crlf after column 4. That is to say, I want the captured data to be many rows of 4 data items, not 1 row of all the data items.

    Any ideas? Any good language reference manuals out yet for .NET 2005?
    Last edited by Pasvorto; Apr 14th, 2006 at 10:10 AM.

  2. #2

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Listview columns

    I tried this:

    VB Code:
    1. For count As Integer = 1 To ListView1.Items.Count - 1
    2.             wholeRow += ListView1.Items(count).SubItems(0).Text & ControlChars.Tab
    3.             wholeRow += ListView1.Items(count).SubItems(1).Text & ControlChars.Tab
    4.             wholeRow += ListView1.Items(count).SubItems(2).Text & ControlChars.Tab
    5.             wholeRow += ListView1.Items(count).SubItems(3).Text
    6.             wholeRow += ControlChars.CrLf
    7.             My.Computer.Clipboard.SetText(wholeRow)
    8.         Next

    However, it doesn't like the 'subitems(x)' format.

  3. #3

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Listview columns

    OK. This code will get me the column data, but it only does if ro the 1st row..

    VB Code:
    1. For count As Integer = 0 To ListView1.Items.Count - 1
    2.             For count2 = 0 To ListView1.Columns.Count - 1
    3.                 wholeRow += ListView1.Columns(count2).Text & ControlChars.Tab
    4.             Next
    5.             wholeRow += ControlChars.CrLf
    6.         Next
    7.         My.Computer.Clipboard.SetText(wholeRow)

  4. #4

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Listview columns

    Finally got it... Here is the finished code:
    VB Code:
    1. My.Computer.Clipboard.Clear()
    2.  
    3.         Dim wholeRow As String = ListView1.Text
    4.         Dim count2 As Integer
    5.  
    6.         For count As Integer = 0 To ListView1.Items.Count - 1
    7.             wholeRow += ListView1.Items(count).Text & "" & ControlChars.Tab
    8.             For count2 = 1 To ListView1.Columns.Count - 1
    9.                 wholeRow += ListView1.Items(count).SubItems(count2).Text & "" & ControlChars.Tab
    10.             Next
    11.             wholeRow += ControlChars.CrLf
    12.         Next
    13.         My.Computer.Clipboard.SetText(wholeRow)
    14.  
    15.         MsgBox("Data extracted", MsgBoxStyle.Information)

    The problem: The listview has 4 columns defined. At the end of the grid I had a row which just has "=" in the 1st column and notother data in the row. The code that filled the listview did not put anything in the subitems for that. When I went back to the code and added lines to fill subitems with "", it worked. I tried a test on Null on the column, but I couldn't get that to function properly. Anyway, it is working now.

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