Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.Net Capture datagridview cellclick

Posted on 2013-11-18
3
Medium Priority
?
560 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline

885 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