Results 1 to 15 of 15

Thread: Display entire row from one peice of data

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Display entire row from one peice of data

    Hi everyone,
    I am using Visual Studio 2008 Express Edition.

    On my opening form I have list that displays the first column of my database.
    Does anybody know how I can do the following?:
    When I double click an item from the list (or select it and click an open button), the entire row of the database is displayed in another form I have created as a display template.
    All help is greatly appreciated.
    Thanks for looking.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    I assume the first column you are displaying in the list is the primary key/ unique key for the table in your database.
    So on double click, just pass that value to the other form. The other form should query your database for that value and show the entire record.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi thanks for getting back to me.

    I dont think what i am displayin in my list is the primary key for the database.
    My database has the following fields:

    Id Fault Description Details etc....

    The list is displaying the Fault column.

    Please could you tell me how to pass the vlaue on to the other form?

    Many thanks.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    Since the listbox items collection is of object type, you can store anything in it. So store both the things.. display text (Fault) as well as the value (ID).

    Add a class like this:
    vb.net Code:
    1. Public Class MyListItem
    2.     Public DisplayText As String
    3.     Public Value As Integer
    4.     Public Sub New(ByVal displayText As String, ByVal value As Integer)
    5.         Me.DisplayText = displayText
    6.         Me.Value = value
    7.     End Sub
    8.     Public Overrides Function ToString() As String
    9.         Return Me.DisplayText
    10.     End Function
    11. End Class

    Then you can fill your listbox with objects of this class.
    e.g.
    vb.net Code:
    1. ListBox1.Items.Add(New MyListItem("whatever display text", whateverValue))

    In your other form, overload the New method (constructor) to accept a value.
    vb.net Code:
    1. Public Sub New(ByVal ID As Integer)
    2.     InitializeComponent()
    3.  
    4.     'do whatever you way you want to process the ID here
    5.     'get data from database, fill controls with those values etc.
    6.  
    7. End Sub

    And call that form, something like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim selectedId As Integer = CType(List1.SelectedItem, MyListItem).Value
    3.     Dim f As New Form2()
    4.     f.ShowDialog()
    5. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi...

    So i have added the following to my code:

    Public Class MyListItem
    Public DisplayText As String
    Public Value As Integer
    Public Sub New(ByVal displayText As String, ByVal value As Integer)
    Me.DisplayText = displayText
    Me.Value = value
    End Sub
    Public Overrides Function ToString() As String
    Return Me.DisplayText
    End Function
    End Class

    Then where the list box is:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    I have added the following before End Sub:
    ListBox1.Items.Add(New MyListItem("whatever display text", whateverValue))

    What do i have to chenage "whatever display text" and "whatever value" to?

    Many thanks for your help i really appreciate it.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    Quote Originally Posted by leemp5 View Post
    Then where the list box is:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    I have added the following before End Sub:
    ListBox1.Items.Add(New MyListItem("whatever display text", whateverValue))

    What do i have to chenage "whatever display text" and "whatever value" to?

    Many thanks for your help i really appreciate it.
    SelectedIndexChanged event is fired when the selection of listbox is changed. You don't add items to the listbox there, isn't it? That add code goes wherever you are populating the listbox with values. Can you show the code you are using to add your listbox items? I'll be able to suggest appropriate changes to it.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi again,
    I have attatched a word file containing the code i have for my form to display the list so far.
    I was going to go to bed so i closed the form but i guess i didnt save the changes as the list is no longer displaying.
    I think i did something with the databinding options in visual basic.
    Thanks.
    Attached Files Attached Files

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    What you posted in that file is the Designer generated code. You need to post the class file where the code you code is present.
    Also not everyone opens attachments. It would be better if you just copy/paste the code here in [CODE]...[/CODE] tags.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Oh sorry about that, im new to all this visual basic.
    Is this the code that you requested?:


    Code:
    Public Class Index
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    
        Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Left = Me.Width / 2 - Panel1.Width / 2
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    End Class

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi again, i have been working with your code and this is what i have came up with so far if it is correct:

    Code:
    Public Class Index
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    
        Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Left = Me.Width / 2 - Panel1.Width / 2
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            ListBox1.Items.Add(New MyListItem("Fault", ID))
        End Sub
    End Class
    
    Public Class MyListItem
        Public DisplayText As String
        Public Value As Integer
        Public Sub New(ByVal Fault As String, ByVal ID As Integer)
            Me.DisplayText = Fault
            Me.Value = ID
        End Sub
        Public Overrides Function ToString() As String
            Return Me.DisplayText
        End Function
    End Class
    This is as far as i have got but it is returning an error stating that the ID in the following line is not declared:

    Code:
    ListBox1.Items.Add(New MyListItem("Fault", ID))
    Thanks

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    Where were you adding the ListBox contents originally (when not using my code)? Surely it was not the ListBox1_SelectedIndexChanged. You have to add that ListBox1.Items.Add statement there.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi, yes i think it was there the code was placed.
    Please forgive me if this is basic for you it is all brand new to me.
    I created the link from the list box to the database in the design view of visual basic, not in the code view.
    Thanks

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi again, i think iv sorted out some confusion. There must have been something wrong with my code as it was not displaying the Fault column in the list box. I have recreated my project and it is now displaying the Fault column in the list box sucessfully. I have put the new code below:

    Code:
    Public Class Index
    
    
        Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DatabaseDataSet.Table1' table. You can move, or remove it, as needed.
            Me.Table1TableAdapter.Fill(Me.DatabaseDataSet.Table1)
    
        End Sub
    End Class
    Thanks

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display entire row from one peice of data

    ok I see now. So you are using databinding. Then use the DisplayMember and ValueMember properties. You do not need anything else.
    You specify which field to use to display items in listbox as DisplayMember, and the field whose value to be used as ValueMember.

    Try this:
    (not tested code)
    vb.net Code:
    1. Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     ListBox1.DataSource = Me.DatabaseDataSet.Table1
    3.     ListBox1.DisplayMember = "Fault"
    4.     ListBox1.ValueMember = "ID"
    5.     Me.Table1TableAdapter.Fill(Me.DatabaseDataSet.Table1)
    6. End Sub



    The rest is almost same with minor modificaitons.

    In your other form, overload the New method (constructor) to accept a value.
    vb.net Code:
    1. Public Sub New(ByVal ID As Integer)
    2.     InitializeComponent()
    3.  
    4.     'do whatever you way you want to process the ID here
    5.     'get data from database, fill controls with those values etc.
    6.  
    7. End Sub

    And call that form, like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim selectedId As Integer = ListBox1.SelectedValue
    3.     Dim f As New Form2(selectedId)
    4.     f.ShowDialog()
    5. End Sub
    Last edited by Pradeep1210; Sep 21st, 2009 at 08:33 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    53

    Re: Display entire row from one peice of data

    Hi Pradeep, its looking good so far.

    What do you mean when you say 'do whatever you way you want to process the ID here 'get data from database, fill controls with those values etc.???

    Does this mean i have to insert the code to get the info from the database and insert it into the text boxes? The only way i know how to populate the text boxes on the view form (form that displays all of the data) is to assign the databindings from the properties menu in visual basic.

    The code for my view page is:
    Code:
    Public Class View
    
        Private Sub View_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Left = Me.Width / 2 - Panel1.Width / 2
        End Sub
        Private Sub Button1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
            Index.Show()
            Me.Hide()
        End Sub
    End Class
    Thanks for your help once again.
    Last edited by leemp5; Sep 22nd, 2009 at 04:55 AM.

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