Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Listbox Exporting And Printing Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Resolved [RESOLVED] [2005] Listbox Exporting And Printing Problem

    My list box is getting it's data from an access database.

    I try to export the contents into a txt file and I get...
    System.Data.DataRowView
    System.Data.DataRowView
    System.Data.DataRowView
    System.Data.DataRowView
    System.Data.DataRowView

    I get the same thing when I try to print it out.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Listbox Exporting And Printing Problem

    You are presumably just looping through the items and calling ToString on them. If your ListBox is bound to a DataTable then each item is a DataRowView. What you're seeing IS the result of calling ToString on a DataRowView. If what you actually want is the text displayed in the ListBox for each item then you need to loop through the item indexes and call GetItemText for each one. The documentation for the ListBox class could have told you that and that's the first place you should have looked. The documentation for the type or member you're using and having trouble with should always be the first thing you look at.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: [2005] Listbox Exporting And Printing Problem

    Sorry I am pretty much a noob, but I got it somewhat working, but now it exports/prints only the first entry in the list box.

    I've looked for solutions but couldn't find any

    Code:
    Me.lstAccounts.DataSource = ds.Tables(0)
                Me.lstAccounts.DisplayMember = "Username"
                Me.lstAccounts.Text = Me.lstAccounts.GetItemText(Me.lstAccounts.DataSource)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Listbox Exporting And Printing Problem

    If you were getting multiple lines of output before you must have been using a loop. You still need to use a loop. You need to call GetItemText for each item in the control. NOT for the DataSource, for each item in the control. Take the highlight as a clue as to what sort of loop you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: [2005] Listbox Exporting And Printing Problem

    Quote Originally Posted by jmcilhinney
    If you were getting multiple lines of output before you must have been using a loop. You still need to use a loop. You need to call GetItemText for each item in the control. NOT for the DataSource, for each item in the control. Take the highlight as a clue as to what sort of loop you need.
    Thx I get what u r saying, but i tried a couple of things and i can't get the loop to work properly.

  6. #6
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    Re: [2005] Listbox Exporting And Printing Problem

    Hello,

    If possible, could you please post what code you have so far? Someone here will be able to help you more if they see what you are trying to do versus what you are actually doing.

    Basically from what I see, you have a table. ds.tables(0)
    This table has rows. You need to loop through these rows, reading the data you want off of the row.

    Very short example of what I'm thinking might be your root issue:

    Code:
    dim dt as new DataTable = ds.tables(0)
    
    For Each dr as DataRow In dt.rows '// This is the For Each loop what JM was implying to
    
    '//Read from the row and assign whatever you need to here
    
    Next dr
    Using Framework 2.0, VB.Net 2005.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: [2005] Listbox Exporting And Printing Problem

    Quote Originally Posted by Crushinator
    Hello,

    If possible, could you please post what code you have so far? Someone here will be able to help you more if they see what you are trying to do versus what you are actually doing.

    Basically from what I see, you have a table. ds.tables(0)
    This table has rows. You need to loop through these rows, reading the data you want off of the row.

    Very short example of what I'm thinking might be your root issue:

    Code:
    dim dt as new DataTable = ds.tables(0)
    
    For Each dr as DataRow In dt.rows '// This is the For Each loop what JM was implying to
    
    '//Read from the row and assign whatever you need to here
    
    Next dr
    I just cant figure out how to loop the GetItemText Properly And What The GetItemText should be assigned to e.g Datasource

  8. #8
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    Re: [2005] Listbox Exporting And Printing Problem

    Oh, I thought you were trying to assign to the listview, not read from it. Sorry, that was my fault.

    Hmm, could you do something like:
    Code:
    dim fs as new IO.FileStream("C:\", FileMode.CreateNew)
    dim sw as new IO.StreamWriter(fs)
    
    For i As Integer = 0 To (lstAccounts.Items.Count - 1)
    
    '//Get whatever values and output however you need
    sw.writeline(lstAccounts.items(i).subitems(0).text)
    
    Next i
    
    sw.close()
    fs.close()
    Not sure if something like this is what you're looking for.
    Using Framework 2.0, VB.Net 2005.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: [2005] Listbox Exporting And Printing Problem

    http://kyrepairs.com/AM.wmv
    ^^
    Right Click Save As

    Video Explaining what is wrong and what im confused with.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Listbox Exporting And Printing Problem

    vb.net Code:
    1. For Each item As Object In myListBox.Items
    2.     MessageBox.Show(myListBox.GetItemText(item))
    3. Next
    As the name suggests, GetItemText gets the text for an item. What you do with that text, i.e. String object, is up to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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