Solved

vb.Net Capture datagridview cellclick

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

18 Experts available now in Live!

Get 1:1 Help Now