Solved

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

Posted on 2004-04-27
6
826 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
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: …

708 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

14 Experts available now in Live!

Get 1:1 Help Now