Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,443 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,305 people online right now. Registration is fast and FREE... Join Now!




Pulling data from DataGridView

 
Reply to this topicStart new topic

Pulling data from DataGridView

deere2007
23 Jan, 2008 - 12:20 PM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 32


My Contributions
I have a datagridview that has columns firstName and lastName. How do I pull the first and last name from the selected row?
User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Pulling Data From DataGridView
23 Jan, 2008 - 01:25 PM
Post #2

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
In a cell_click or cell_doubleClick event, you would do something like this:

CODE

variable1 = DataGridView1.SelectedRows(0).Cells("lastName").Value
variable2 = DataGridView1.SelectedRows(0).Cells("firstName").Value


Hope this helps!


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Pulling Data From DataGridView
23 Jan, 2008 - 01:49 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,299



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
To get the value you can use the Item property of the DataGridView. It will take two parameters, the first parameter is the column index and the second parameter is the row index. The indexes will start at 0.

Lets say that column index 3 holds the first name, so you will set the column index to 3 and then to retrieve the row index of the selected row you can use the CurrentCell.RowIndex property to get the row index. Now use the Value property to get at what is stored in the cell at that column and row, Value returns an object so you will need to cast it to a string, depending on how you are using it.

The same logic will apply to getting the last name, you will just need to change the column index to get that specific value. Naturally you would be storing the value returned in a variable, TextBox, etc.
CODE

Me.DataGridView1.Item(3, Me.DataGridView1.CurrentCell.RowIndex).Value.ToString

User is online!Profile CardPM
+Quote Post

deere2007
RE: Pulling Data From DataGridView
25 Jan, 2008 - 06:18 PM
Post #4

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 32


My Contributions
How would I do the same thing from a listview, i've been playing around trying to figure it out with no luck.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Pulling Data From DataGridView
25 Jan, 2008 - 07:59 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,299



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Post your code, that will help a lot in providing you a solution. Use code.gif tags.
User is online!Profile CardPM
+Quote Post

deere2007
RE: Pulling Data From DataGridView
25 Jan, 2008 - 08:18 PM
Post #6

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 32


My Contributions
code to populate the listview

CODE


  For Each customer In customer_array

            aCustomer = customer_array(i)

            Dim MyItem = ListView1.Items.Add(aCustomer.GetCustomerType)

            i = i + 1

            With MyItem
                .SubItems.Add(aCustomer.GetCustomerId)
                .SubItems.Add(aCustomer.GetFirstName)
                .SubItems.Add(aCustomer.GetLastName)
                .SubItems.Add(aCustomer.GetAddress)
            End With
        Next



The part I am stuck on is here, I cant figue out how to pull the selected customer id from the listview.

CODE


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        customerId =

End sub


User is offlineProfile CardPM
+Quote Post

deere2007
RE: Pulling Data From DataGridView
25 Jan, 2008 - 08:45 PM
Post #7

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 32


My Contributions
I think I got it to work with this ....

CODE



Dim cust_array As New ArrayList
        Dim x As String = ListView1.SelectedIndices.Item(0)

        cust_array = customer.GetAll

        aCustomer = cust_array(x)

        customerId = aCustomer.GetCustomerId

        customer_array = customer.GetAllCustID(customerId)

        aCustomer = customer_array(0)





User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month