Solved

vb.Net Capture datagridview cellclick

Posted on 2013-11-18
3
545 Views
Last Modified: 2013-11-24
At the moment I am capturing cellclicks in a datagridview to populate textboxes with values from access db. This is working fine. Now I have added a listview (Listview1) which populates data ok, but I need a way to use the value of the datagridview cellclick to also populate Listview1 based on the value of the cellclick. I guess I am asking how to declare a global variable.

I have included a portion of code that I use to capture the datagridview value and also the Listview1 code. The variable I need to use is 'value' in UserDataGridView. Thanks

UserDataGridView code

Private Sub UserDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles UserDataGridView.CellContentClick
        Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
        Dim NoEdit As Object = UserDataGridView.Rows(e.RowIndex).Cells(1).Value
        Dim InvCnt As Object = UserDataGridView.Rows(e.RowIndex).Cells(2).Value
        Dim InvAddress As Object = UserDataGridView.Rows(e.RowIndex).Cells(3).Value
        Dim Email As Object = UserDataGridView.Rows(e.RowIndex).Cells(4).Value
        Dim Tel As Object = UserDataGridView.Rows(e.RowIndex).Cells(5).Value
        Dim Fax As Object = UserDataGridView.Rows(e.RowIndex).Cells(6).Value
        Dim Balance As Object = UserDataGridView.Rows(e.RowIndex).Cells(7).Value

        txtCustomerActive.Text = CType(value, String)
        txtCustomerNoedit.Text = CType(NoEdit, String)
        txtInvoiceContact.Text = CType(InvCnt, String)
        txtInvoiceAddress.Text = CType(InvAddress, String)

End Sub

Open in new window


listview code

Sub filllistview()
        Try
            'creatconn()
            oledbCnn.ConnectionString = My.Settings.storageConnectionString
            oledbCnn.Open()
            Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings ORDER BY [Ref no] desc", oledbCnn)
            dr = oledbCmd.ExecuteReader()

            Dim tmpColor As Color = Color.Green

            While dr.Read()


                ListView1.Items.Add(CDate(dr(4)).ToShortDateString()).UseItemStyleForSubItems = False
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
                With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C"))
                    .ForeColor = tmpColor
                End With
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            oledbCnn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:peter-cooper
[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
  • 2
3 Comments
 
LVL 40
ID: 39658318
First of all, do not specify vbscript as a topic when you are asking questions about .NET. VBScript has nothing to do with .NET, and you end up losing a topic that ends up to experts that might not have the proper expertise.

Global variable are overused. There is very little reason to use one nowadays. When somebody declare a global variable, it is usually because he does not know how to do something properly.

If possible, pass the value to filter on as a parameter to filllistview : Sub filllistview(value As Integer) or any other type needed.

If both the DataGridView and the ListView are in the same form, simply define a variable in the Declaration section of the form. This will be visible everywhere in that form.

If they are in different forms, then put filllistview as a Public method in the form that contains the ListView, and call that method through the form instance: YourForm.filllistview(valueToFilterOn)

And from what you say, you want to filter on the value in the click, so your SQL should have a WHERE clause that enables you to filter:

("Select TOP 100 * from Postings WHERE [FieldName] = " & value.ToString & " ORDER BY [Ref no] desc",
0
 

Accepted Solution

by:
peter-cooper earned 0 total points
ID: 39659014
Thank you for your comments.  I am a new user to vb.Net and still trying to grasp the basics coming from a php background.

I have declared variable in the form class like this:

Dim value As Object

Open in new window


I then need to fill this var with the date from a DGV cellclick event like so:

value = UserDataGridView.Rows(e.RowIndex).Cells(0).Value

Open in new window


And finally, make a db query in my Listview1 and populate the listview with the results.

Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings WHERE Customer = '" & value.ToString & "' ORDER BY [Ref no] desc", oledbCnn)

Open in new window


However, the value var which I declared does not contain any data. Hope that is clear. Thank you
0
 

Author Closing Comment

by:peter-cooper
ID: 39672355
I managed to solve by myself
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get sourcecode path 14 48
YouTube API get the Video Tags - vb.net 7 33
Nested forach loop to linq 3 30
Blending HTML output from R Studio into a ASP.Net/C# project 4 23
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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