Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

pass values from datagrid on form2 to listview on form1,help choose a suitable control

vb.net2003,access2000,oledb
good day i have a datagrid that is filled via a dataset, which i populated in code in list 1. the order of actions are as

follows:
load form1
user selects fill customers or fill suppliers button

a oledbcommand is set (cmd1)
a oledbdataadapter is created (da1.selectcommand = cmd1)
and oledbcommand is set

a dataset is created an filled with dataadapter
da1.fill(ds1)

a new form2 is instantiated and its dataset is set to
dataset from form1
form2 sets the datagrids datasource to the dataset (in  code)
grid1.datasource = ds1
form2 is displayed.
all works fine so far.

now a few questions
the user now clicks on a row on the datagrid on form2.

QUESTION1
----------
i need to save the columns 1,2,3 of the row the user selected (into memory?)
how do i do this, do i user the grids navigation event and somehow get the data of the selected row?

now the form2 disposes and all the details of columns 1,2,3 are displayed in a listview on form1

Question2
-------------------------------------------
Customer Name
Customer Address
Customer Telephone Number
-------------------------------------------

these are a sample of what would be inserted from form2 to form1.
should i use a listview for the selected control?
and listview.listitems.add?

Question3
----------
i would like to make use of objects in the project.(i come from vb6)
im not too hot on OO yet but i reckon the more i use it the better i will get.
should i create a class CONTACT and create these 3 objects and then pass the values?
Name
Address
TelephoneNumber

because i will use the form with the customers replaced with Suppliers later in the project and then i will also
need
Name
Address
TelephoneNumber

i know this is a lot, but hopefully someone can help,
thanks!
0
jxharding
Asked:
jxharding
  • 3
  • 3
1 Solution
 
tgannettsCommented:
Suggestions:

I think you are right to create a class called Contact which you can then use to create a property on the form to hold the user's selection, so the following code could be added to Form2:

    Public Class Contact
        Public Name As String
        Public Address As String
        Public TelephoneNumber As String
    End Class

    Private m_SelectedContact As Contact

    Public Property SelectedContact() As Contact
        Get
            Return m_SelectedContact
        End Get
        Set(ByVal Value As Contact)
            m_SelectedContact = Value
        End Set
    End Property

In the New sub of Form2 remember to add the constructor call:

Me.SelectedContact = new Contact()

Then you can add a handler for the Mouse Down event on the Datagrid on Form2 to determine which row if any the user has selected using the HitTestInfo class:

Public Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles DataGrid1.MouseDown

        Dim ht As DataGrid.HitTestInfo

        ' Use the DataGrid control's HitTest method with the x and y properties.
        ht = DataGrid1.HitTest(e.X, e.Y)

        'Check if row has been selected
        If ht.Row <> -1 Then
           
            Me.SelectedContact.Name = Me.DataGrid1.Item(ht.Row, 0).ToString
            Me.SelectedContact.Address = Me.DataGrid1.Item(ht.Row, 1).ToString
            Me.SelectedContact.TelephoneNumber = Me.DataGrid1.Item(ht.Row, 2).ToString

            'Close form
            Me.DialogResult = DialogResult.OK

        End If

End Sub

If you have set the form to be displayed modally, setting the dialog result will close the form automatically.

So from form 1 in the sub where Form 2 was displayed from you can add the following code:

Dim frm2 as new Form2(ds)
Dim strName as string

if frm2.ShowDialog = DialogResult.OK then
         'Get Details from SelectedContact Property
         strName = frm2.SelectedContact.Name
Else
End if    
0
 
tgannettsCommented:
If you want to use a Listview to display the selected details then you can simply create a listview item and add it to that using the values held in the Contact class:

Dim lvi as ListViewItem
Dim frm2 as new Form2(ds)

If frm2.ShowDialog = DialogResult.OK Then
     lvi = new ListViewItem(frm2.SelectedContact.Name)
     lvi.SubItems.Add(frm2.SelectedContact.Address)
     lvi.SubItems.Add(frm2.SelectedContact.TelephoneNumber)
     Me.ListView1.Items.Add(lvi)
End If

Hope this is of some help.

Tom
0
 
jxhardingAuthor Commented:
thank you tom, im logging off now will try everything you suggested after hours and return
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jxhardingAuthor Commented:
hi tom, everyting i received  so far is excellent!
could u just give me a pointer on this one line please, im lost as to the new sub:
---------------------------------------------------------------------
In the New sub of Form2 remember to add the constructor call:
Me.SelectedContact = new Contact()
---------------------------------------------------------------------

thanks!
0
 
tgannettsCommented:
In the Windows Form Designer Generated code region for each form you create, you will find the Public Sub New() subroutine (Remember every form you use is just an inherited class).
This will run every time you create a new instance of the form, for example 'Dim frm as New Form2()'

To enable the use of the Contact class as a property, you will need to initialize the class otherwise you will receive an Object Reference error. So, in the code for the Public Sub New(), you will find a message automatically generated by the IDE saying 'Add any initialization after the InitializeComponent() call'. Here you can put any code you want to run every time a new instance of the form is created. Add the 'Me.SelectedContact = new Contact()' here.

Tom
0
 
jxhardingAuthor Commented:
i learnt an immense lot!
thanks a million!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now