Solved

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

Posted on 2004-04-27
6
830 Views
Last Modified: 2010-05-18
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
Comment
Question by:jxharding
  • 3
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
tgannetts earned 500 total points
ID: 10927397
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
 
LVL 5

Expert Comment

by:tgannetts
ID: 10927447
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
 

Author Comment

by:jxharding
ID: 10927599
thank you tom, im logging off now will try everything you suggested after hours and return
0
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.

 

Author Comment

by:jxharding
ID: 10928053
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
 
LVL 5

Expert Comment

by:tgannetts
ID: 10928219
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
 

Author Comment

by:jxharding
ID: 10936906
i learnt an immense lot!
thanks a million!
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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

21 Experts available now in Live!

Get 1:1 Help Now