Solved

How to open form from datagridview

Posted on 2009-04-14
5
365 Views
Last Modified: 2012-05-06
Hello,

I have a query that populates a DataGridView. I'm trying to acquire some code to get it to open a databound form when the user selects (double clicks) a row. I have a unique identifier which pulls using e.rowindex. I've completed this successfully in VB/VBA. Any ideas on this will be a huge help! The query is pretty simple. "PendingID" is the unique identifier.

This is written in VB.NET

Thanks!!!
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
 
        Dim OptControlArray() As Control = {OptName, OptClaimNo, OptDate, OptCustodian}
        Dim sConnectionString, sSQL As String
        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\diw07\My Documents\Daily Backups\phone memo backend.mdb"
 
        If OptClaimNo.Checked = True Then
            sSQL = "SELECT PendingID, PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE PendingClaimNum Like '" & txtClaimNo.Text & "'"
        ElseIf OptName.Checked = True Then
            sSQL = "SELECT PendingID, PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE Last Like '" & txtLastName.Text & "'"
        ElseIf OptCustodian.Checked = True Then
            sSQL = "SELECT PendingID, PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE Last Like '" & cboCustodian.Text & "'"
        ElseIf OptDate.Checked = True Then
            sSQL = "SELECT PendingID, PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE Last Like '" & txtDateRec.Text & "'"
        End If
 
        Dim conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
        Dim cmd As New System.Data.OleDb.OleDbCommand(sSQL, conn)
        conn.Open()
        Dim dat As OleDbDataAdapter = New OleDbDataAdapter(sSQL, conn)
        Dim dt As New DataTable()
 
        dat.Fill(dt)
        lstResults.DataSource = dt
        conn.Close()
 
 
    End Sub
 
 
 
 
 
 
 
********Need something here************
 
 Private Sub lstResults_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles lstResults.CellMouseDoubleClick
        Dim I As Int32
        I = e.RowIndex
 
Another query here?
        
        frm_IMT.Show()
       
    End Sub

Open in new window

0
Comment
Question by:c9k9h
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24145145
DataGridView with Detail Edit Form - VS 2005
http://www.codeproject.com/KB/vb/DataGridViewEditForm.aspx
Try this:
 
Private Sub dataGridView1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) 
    Dim id As String = dataGridView1.SelectedRows(0).Cells("id").ToString() 
    Dim frm As New NewForm(id) 
    'passing id into constructor of the new form 
    frm.Show() 
End Sub 

Open in new window

0
 

Author Comment

by:c9k9h
ID: 24147564
I've tried this but I'm getting an error "Too many arguments to Public Sub New()"


Also... won't this create a new form? I want to open an existing form and have it pull the record displayed in the DataGridView.


Thanks!
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24147606
>> I've tried this but I'm getting an error "Too many arguments to Public Sub New()"

You'll need to create a constructor which accepts the id as a parameter. When you pass the id, you can retrieve the data in your form_load event
0
 

Author Comment

by:c9k9h
ID: 24148183
Okay I found the following example:

Module Module1
                                             
                                             Sub Main()
                                             
                                             Dim con As New Constructor(10)
                                             WriteLine(con.display())
                                             'storing a value in the constructor by passing a value(10) and calling it with the
                                             'display method
                                             Read()
                                             
                                             End Sub
                                             
                                             End Module
                                             
                                             Public Class Constructor
                                             Public x As Integer
                                             
                                             Public Sub New(ByVal value As Integer)
                                             'constructor
                                             x = value
                                             'storing the value of x in constructor
                                             End Sub
                                             
                                             Public Function display() As Integer
                                             Return x
                                             'returning the stored value
                                             End Function
                                             
         End Class                                        



Ultimately what i want to do is assign a value to "id" which would be the results of the on click event (query)? I understand (i think) what is going on in the above example, although it seems quite simple in comparison of what I'm trying to do.
0
 

Author Closing Comment

by:c9k9h
ID: 31570240
Got It,

Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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