Solved

vb.Net Capture datagridview cellclick

Posted on 2013-11-18
3
539 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 53
Runtime Error 2 28
SQL Exceptions 3 37
VB.NET Repostiory Pattern 7 8
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This is an introductory video for CloudBerry Managed Backup. You will learn how to sign up with the service and get started in a few minutes.

911 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

20 Experts available now in Live!

Get 1:1 Help Now