Solved

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

Posted on 2014-02-07
5
347 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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