Solved

vb.Net Capture datagridview cellclick

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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