Learn how to a build a cloud-first strategyRegister Now

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

windows applications in visual basic 2005

Hi,
what I have is a search window that display the results via the datagrid.  I have a few textboxes that show specific information when I single left click on a particular datagridrow.  What I would like to know how to do is "double" clicking on a particular row within the datagridview and for the record of that employee (for example) to display on my addclientform where the employees are created.
And when the info populates into this other form the search employee form closes.
Can anyon please help me with this or point me into a right direction.
I do not know how to google this question, is there a specific wording for it?
thanks
0
systems_ax
Asked:
systems_ax
  • 17
  • 13
1 Solution
 
VBRocksCommented:
In your "addclientform", put this sub at the top:

    Public Sub New(ByVal rows As DataGridViewRow())
        Me.InitializeComponent()

        'Loop through all of the selected rows and get your values.
        For Each row As DataGridViewRow In rows
            Debug.WriteLine(row.Cells(0).Value)
        Next

    End Sub



'Then on your form that has the DataGridView, use this code:
        If Me.DataGridView1.SelectedRows.Count > 0 Then
            Dim row As DataGridViewRow = _
                Me.DataGridView1.SelectedRows(0)

            Dim frm As New addclientform(row)
            frm.Show()

        End If



0
 
VBRocksCommented:
Oops, change this to the following:

'Then on your form that has the DataGridView, use this code:
        If Me.DataGridView1.SelectedRows.Count > 0 Then

            Dim frm As New addclientform(Me.DataGridView1.SelectedRows)
            frm.Show()

        End If

0
 
VBRocksCommented:
Sorry, I just noticed the Sub New requires something a little different, so use this instead:

    Public Sub New(ByVal rows As DataGridViewSelectedRowCollection)
        Me.InitializeComponent()

        For Each row As DataGridViewRow In rows
            Debug.WriteLine(row.Cells(0).Value)
        Next

    End Sub



And what I posted just above:
'Then on your form that has the DataGridView, use this code:
        If Me.DataGridView1.SelectedRows.Count > 0 Then

            Dim frm As New addclientform(Me.DataGridView1.SelectedRows)
            frm.Show()

        End If


0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
systems_axAuthor Commented:
VBRocks,
thank you very much.
I did input the code but now my main form does not like the "declaration statement to show the add client form"
This is what I have on my main form:
Private Sub btnClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClient.Click
        Dim frm As New AddClient===has ~~~ under "frm"
        frm.Show()
    End Sub

Is there supposed to be a conflict, or am I missing "any application settings"

is it because I make declaration on my search for clients form:
Private Sub dgvLastNames_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvLastNames.DoubleClick
        If Me.dgvLastNames.SelectedRows.Count > 0 Then
            Dim frm As New AddClient(Me.dgvLastNames.SelectedRows)
            frm.Show()
        End If
    End Sub
0
 
systems_axAuthor Commented:
I put the sub new under the Public Class AddClient, correct?
thank you
0
 
systems_axAuthor Commented:
VBRocks,
PLEASE HELP
0
 
systems_axAuthor Commented:
VBRocks,
can you tell me an acronym by which I can search as your code is not working.
thank you
0
 
VBRocksCommented:
systems_ax, my apolgies for not getting back with you, I've been swamped!  I'll take a look at this in the
morning.

0
 
VBRocksCommented:
Hi systems_ax, sorry for the delay in getting back with you.

The code example below is treating "addclientform" as the name of the form that you want to open.
If that is not the correct name for your form, then use the correct name instead.  
For example, is "AddClient" the name of your form?  If it is, then the example would be:

            Dim frm As New AddClient(Me.DataGridView1.SelectedRows)
            frm.Show()


This portion of code looks correct, does it work for you?
Private Sub dgvLastNames_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles dgvLastNames.DoubleClick

        If Me.dgvLastNames.SelectedRows.Count > 0 Then

            Dim frm As New AddClient(Me.dgvLastNames.SelectedRows)
            frm.Show()

        End If

    End Sub

0
 
systems_axAuthor Commented:
VBRocks,
thank you for your reply but I still cannot get it to work.  I will start from the beginning as I do not think I explained my problem in detail as I should have:

I have 1 Main form with 1 button on it, when I click that button it opens a form where the user can enter a new client.  This "addnewclient" form does not have a datagridview and has about 20 textboxes where the user can enter new data for a specific cleint.
Then I am able to save this client successfully.
Then on this addclintfrom I have a button that opens another window, which is a "searchclient" window that also has a button and when I click that button "show all cleints" the datagridview displays all the clients".  I have 7 textboxes undernearth this dgview and when I single click on a particular row within the dgview specific information is displayed within the textboxes underneath which pertain to the client that I single clicked on.
What I need is to be able to double click on a particular client within this dgview and for the "addclient" form to open where this client was created originally and I need all the textboxes that the user had filled for this client to be filled and for the "search client" form to close.

THIS IS WHAT i HAVE BY FOLLOWING YOUR INSTRUCTIONS:
1) on my main form I have this code:
Private Sub btnClientProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClientProfile.Click
        Dim frm As New AddClient(frmClients.dgvLastNames.SelectedRows)
        frm.Show()
    End Sub

2) on my add to client form I have this under the Public Class AddClient, IS THAT CORRECT?

Public Sub New(ByVal rows As DataGridViewSelectedRowCollection)
        Me.InitializeComponent()

        For Each row As DataGridViewRow In rows
            Debug.WriteLine(row.Cells(0).Value)
        Next

    End Sub

3) I HAVE THIS On my search client form:
 Private Sub dgvLastNames_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLastNames.CellDoubleClick
        If Me.dgvLastNames.SelectedRows.Count > 0 Then

            Dim frm As New AddClient(Me.dgvLastNames.SelectedRows)
            frm.Show()
        End If

    End Sub

please help, what I am doing wrong?
0
 
VBRocksCommented:
So in the add to client form, use the first row that was selected to retrieve you data and load your
textboxes:

Public Sub New(ByVal rows As DataGridViewSelectedRowCollection)
        Me.InitializeComponent()

       'For example (referencing the column index in Cells(0)
       Me.TextBox1.Text = rows(0).Cells(0).Value

    End Sub

0
 
systems_axAuthor Commented:
VBRocks,
do I have to set this function for each textbox individually, such as:
Me.firstname.Text = rows(0).Cells(0).Value
Me.lastname.Text = rows(0).Cells(0).Value
Me.middlename.Text = rows(0).Cells(0).Value
Me.address.Text = rows(0).Cells(0).Value

thank you very much, I will try it right now
0
 
systems_axAuthor Commented:
i have an error that says "argument out of range exception was unhandled.  "index wa sout of range. must be non-negative or less than the size of the collection.  paramater name index"

must I know the specific row and a cell pertaining to each textbox?
please advise or are there any  tutorials on this matter as I cannot find anything.
0
 
VBRocksCommented:
Sorry.  Since you are working with a collection, then use this:

Me.firstname.Text = rows(1).Cells(0).Value
Me.lastname.Text = rows(1).Cells(0).Value
Me.middlename.Text = rows(1).Cells(0).Value
Me.address.Text = rows(1).Cells(0).Value

'Also, you do need to know either the name of the column that each cell belongs to, or it's index.
'If you know the column, then you can use this:
    Me.firstname.Text = rows(1).Cells("FirstName").Value

0
 
systems_axAuthor Commented:
VBRocks,
I tried it and the code breaks with "Index was out of range.  must be non-negative & less than the size of the collection.  Parameter name:index."  The error hangs at the first line that says "Me.txtFirst.Text = rows(1).Cells("ClientFirstName").Value.

I apologize I am not knowledgable in this area, so I have no other assistance than your assistance.
how do I google this question so that I can find a book or an article on it.
the code still does not work  and I have the code below as an active code:

    Public Sub New(ByVal rows As DataGridViewSelectedRowCollection)
        Me.InitializeComponent()
        Me.txtFirst.Text = rows(1).Cells("ClientFirstName").Value
        Me.txtLast.Text = rows(1).Cells("ClientLastName").Value
        Me.txtMiddle.Text = rows(1).Cells("ClientMiddleName").Value
        Me.txtDate.Text = rows(1).Cells("ClientDateofBirth").Value
        Me.txtAddress.Text = rows(1).Cells("ClientStreetAddress").Value
        Me.txtPhone.Text = rows(1).Cells("ClientTelephoneNumberOne").Value
        Me.txtDateSeen.Text = rows(1).Cells("ClientDateSeen").Value
        Me.txtComments.Text = rows(1).Cells("ClientComments").Value
        Me.txtReports.Text = rows(1).Cells("Reports").Value
        Me.txtEmail.Text = rows(1).Cells("ClientEmail").Value
        Me.txtCompany.Text = rows(1).Cells("ClientCompany").Value
        Me.txtCity.Text = rows(1).Cells("ClientCity").Value
        Me.txtState.Text = rows(1).Cells("ClientState").Value
        Me.txtPostal.Text = rows(1).Cells("ClientPostalCode").Value
        Me.txtCountry.Text = rows(1).Cells("ClientContry").Value
        Me.txtPhone2.Text = rows(1).Cells("ClientTelephoneNumberTwo").Value
        Me.txtFax.Text = rows(1).Cells("ClientFaxNumber").Value
end sub

thank you
0
 
VBRocksCommented:
Not a problem!  Your code looks good.  Are you sure you are selecting a complete row before
processing?  (You may need to click on the far left of the grid to select the complete row).

0
 
systems_axAuthor Commented:
VBRocks,
sorry, I did not specify that the error in my previous post is generated after I click on a button that is supposed to open this form.  So, is there any way, you can upload a working example for me or direct me to a helpful tutorial.
thank you
0
 
VBRocksCommented:
Sure.  Give me just a bit.
0
 
systems_axAuthor Commented:
thank you very much
0
 
VBRocksCommented:
0
 
systems_axAuthor Commented:
VBRocks,
is there anyway to make it a bit more simple such as excluding the uncessesary forms, I am still trying to understand how this function works and all the extra confuses me.
I am going to try and delete some of the coding and 1 extra form and see if I can understand.
thank you
0
 
systems_axAuthor Commented:
My datagrid is not bound also, and I use this function to populate my datagridview:

Private Sub Populate_Information2(ByVal ClientLastName As String)
        dgvLastNames.Columns.Clear()
        OpenDB()
        ClientLastName = "'%" & ClientLastName & "%'"
        Dim Sql As String = "SELECT ClientFirstName, ClientMiddleName, ClientLastName FROM Clients"
        Dim cmd As New OleDbCommand(Sql, Conn)
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim dt As DataTable
        da.SelectCommand = cmd
        Try
            da.Fill(ds, "Data")
            dt = ds.Tables("Data")
            dgvLastNames.DataSource = dt
            Dim x As Integer
            For x = 0 To dt.Columns.Count - 1
                Dim dgc As New DataGridViewTextBoxColumn
                With dgc
                    .Name = dt.Columns(x).ColumnName.ToString
                    .DataPropertyName = dt.Columns(x).ColumnName.ToString
                    .HeaderText = dt.Columns(x).ColumnName.ToString
                    .Width = 100
                End With
                dgvLastNames.Columns.Add(dgc)
            Next
            dgvLastNames.DataSource = dt
            If x <= 0 Then
                MessageBox.Show("No Record Found!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MsgBox("Error while executing: (" & cmd.CommandText & ")" & vbCrLf & "Msg: " & ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        Finally
            da = Nothing
            ds = Nothing
            dt = Nothing
            cmd.Dispose()
            CloseDB()
        End Try
    End Sub
0
 
VBRocksCommented:
First of all, I think you can start by simplifying this sub:

Private Sub Populate_Information2(ByVal ClientLastName As String)
        dgvLastNames.Columns.Clear()
        OpenDB()
        ClientLastName = "'%" & ClientLastName & "%'"
        Dim Sql As String = "SELECT ClientFirstName, ClientMiddleName, ClientLastName FROM Clients"
        Dim cmd As New OleDbCommand(Sql, Conn)
        Dim da As New OleDbDataAdapter

        Dim dt As DataTable
        da.SelectCommand = cmd
        Try
            da.Fill(dt)
            dgvLastNames.DataSource = dt

            If dt.Rows.Count = 0 Then
                MessageBox.Show("No Record Found!", "Message", MessageBoxButtons.OK, _
                   MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MsgBox("Error while executing: (" & cmd.CommandText & ")" & vbCrLf & "Msg: " & _
                ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        Finally
            da = Nothing
            dt = Nothing
            cmd.Dispose()
            CloseDB()
        End Try
    End Sub

0
 
systems_axAuthor Commented:
I clear the columns first as if I do not there will be duplicate columns.
then, I open the database
I indicate that I can search ONLY for a last name.  For instance, if I type in "s" and hit search, the datagridview will find ALL the records with that letter within the last name in it.
Then, my datagridview ONLY Has 3 columns, which are visible within the grid, and when I hit "show all" these columns populate with data.
Then I close the database.

so the function is very different from yours and that is where the problem is.  As I did follow your instructions prior to your upload.  The upload confuses me a bit as there are very many code lines that can be taken out.

Thank you,
I am still going through your upload.
0
 
systems_axAuthor Commented:
VBRocks,
thank you.
I made it work the way I needed it to work.  How do I close the search form before the add form comes up?  
thank you
0
 
VBRocksCommented:
Just use Me.Close right after you open the form...

However, for that to work, you need to change the following setting:
    Project Menu | <Project> Properties, Application Tab, and set the "Shutdown Mode" to
        "When last form closes"

0
 
systems_axAuthor Commented:
VBRocks,
I tried that and I might have missed something.
I put the "Me.close" under:

Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        Dim frm As New PopUp(Me.DataGridView1.SelectedRows)
        frm.ShowDialog()
        Me.Close()
    End Sub

and set the Project Menu | <Project> Properties, Application Tab, and set the "Shutdown Mode" to
        "When last form closes".
I get the same result, my datagrid form stays behind the add form.
Maybe I did not explpain it very well.  When I double click on the datagridview, I need the add form to open and the main form to close automatically or just transition away.
thank you
0
 
VBRocksCommented:
Try this:

    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) _
       Handles DataGridView1.DoubleClick

        Dim frm As New PopUp(Me.DataGridView1.SelectedRows)
        frm.Show()    '< Changed to "Show()"
        Me.Close()

    End Sub



0
 
systems_axAuthor Commented:
VBRocks,
I have 1 more question please before I assing the points to you.
I would like to display ONLY 3 columns within my datagrid and have every column listed after the addtoclient form pops up upon double click of the search form.

However, what I have found is that I need to include every column within my SQL statement underneath for the field t be included within the second form "addtocleint form".  I have tried   setting the column that I do not want to display within the datagridview to "invisible" status, but it does not work, the column still shows.  This is what I do.  I select the datagridview, go to "columns", select the column that I do not want to see within the datagridview during run time, change its visible property to "false".

Dim Sql As String = "SELECT ClientFirstName, ClientMiddleName, ClientLastName FROM Clients"

is there a work around this issue, should I set the property to false within the code on load of the main form?
0
 
systems_axAuthor Commented:
never mind
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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