Solved

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

Posted on 2014-02-07
5
346 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
ID: 39844252
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
ID: 39844285
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
ID: 39845340
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
ID: 39847988
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
ID: 39857415
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

19 Experts available now in Live!

Get 1:1 Help Now