Solved

How do I Define and Use a datarow in a class in VB.NET

Posted on 2014-02-07
5
344 Views
Last Modified: 2014-02-13
I am trying to create a "Search" form.  The user will select a customer on this form, and prior to the form closing, I would like to select a Customer datarow (strongly typed), so that the previous form that was calling this "Search"form would be able to read this datarow.  I can pass standard variables (string, long, etc) just fine as I have done with "Name" in the code below (lines 4-12), but I am not sure how to do this with a strongly typed datarow.  If it isn't possible with a datarow but a datatable would work, I could work with that as well.

Public Class frmCustomerSearch

    'This works fine
    Private m_Name As String = ""
    Friend Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    'Would like to do something like the following
    Private m_selectedCustomer As String = ""
    Friend Property selectedCustomer() As myDataset.CustomerRow
        Get
            Return m_selectedCustomer 
        End Get
        Set(ByVal value As myDataset.CustomerRow
            m_selectedCustomer = value
        End Set
    End Property

end Class

Open in new window

0
Comment
Question by:eeyo
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I don't completely understand your question.  Are you trying to pass the myDataSet.CustomerRow into a method as a parameter?
0
 

Author Comment

by:eeyo
Comment Utility
A parameter would work.  To keep it clean, I would rather not use a global datatable or datarow.  I would like to call frmCustomerSearch from other winforms, as below:

using frm as New frmCustomerSearch
    'Optional: could possibly assign a value to an element of the the typed datarow selectedCustomer ...
    frm.selectedCustomer.CustomerID = 12345

    frm.ShowDialog()

    'do something with the typed datarow selectedCustomer, such as ...
    Messagebox.show(frm.selectedCustomer.PhoneNumber)
end using

Open in new window

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 100 total points
Comment Utility
If you want to pass the parameter to the form, you can do it a few ways:

1) Define a public property, and set it.

2) Add a parameter to the constructor, and pass it.
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 400 total points
Comment Utility
The strongly typed datarow definition has to be accessible to both forms, so you either need to make it global or make it a Friend definition in the parent form, then qualify the definition in the sub form. So if frmMain is the parent form, and you define myDataSet in frmMain, the property definition in frmCustomerSearch would need to be something like:
    Private m_selectedCustomer As frmMain.myDataset.CustomerRow
    Friend Property selectedCustomer() As frmMain.myDataset.CustomerRow
        Get
            Return m_selectedCustomer 
        End Get
        Set(ByVal value As frmMain.myDataset.CustomerRow
            m_selectedCustomer = value
        End Set
    End Property

Open in new window

However, note that the search that you do in the sub form needs to refer, not to the frmMain definition, but to the instantiated form. The easiest way to refer to it is to have a variable frmOwner in the sub; in the Load event, have the statement

frmOwner = Me.Owner

and in the creation of the sub form in frmMain, before .ShowDialog(), put

frm.Owner = Me

Then in frmCustomerSearch, you would do searches on frmOwner.myDataSet.
0
 

Author Comment

by:eeyo
Comment Utility
Looking at it now, I feel a little silly that I didn't just try to use myDataset.CustomerRow as in the answer.  Thanks for the tip on using Owner.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now