VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

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

Join 300,483 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




Obtaining field value from datagrid on double click

 

Obtaining field value from datagrid on double click

webwired

28 Aug, 2007 - 05:09 AM
Post #1

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
From what I'm finding, there doesn't appear to be any particular function available to obtain the value from a particular field from a double clicked on row of a datagrid...

So if you wanted to double click on a particular result in a datagrid, then pass one or more of the values of that row, you couldn't?

I was thinking something like this, but of course that doesn't work, any ideas?

CODE

    Private Sub InventoryDataGridView_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellContentDoubleClick
        Dim invID As String = InventoryDataGridView.CurrentRow(???ParticularField???)
    End Sub


User is offlineProfile CardPM
+Quote Post


m2s87

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 06:32 AM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 15 times
Dream Kudos: 1225
My Contributions
blink.gif First time for me to use this control in vb.net - lol.
Anyhow i guess you could use something like this:
CODE
    Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
        If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then 'AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell
            MsgBox("Value= " & sender.CurrentCell.EditedFormattedValue())
        End If
    End Sub

    'this is just to show how event works; you do not need to use it
    Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
        Me.Text = "No way: column = " & e.ColumnIndex & " and row=" & e.RowIndex
    End Sub

I am not curtain if one needs to use the preliminary check - if one double clicked item and not the row or column selector, but i think using the type check could be advised.
User is offlineProfile CardPM
+Quote Post

webwired

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 06:57 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
I think I can make that work, that was a pretty nifty idea.

Thank you very much m2s87!

EDIT: LOL, that was until I entered my Option Strict On, then it tells me that "Option Strict On disallows late binding."

This post has been edited by webwired: 28 Aug, 2007 - 07:25 AM
User is offlineProfile CardPM
+Quote Post

webwired

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 10:06 AM
Post #4

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
Does anybody else have any ideas as to how to grab a value from a particular field of a double clicked on datagrid row?

You know how nice it would be if the "http://forums.microsoft.com/MSDN/ShowPost.aspx?" actually worked... Ergh!

This post has been edited by webwired: 28 Aug, 2007 - 10:23 AM
User is offlineProfile CardPM
+Quote Post

webwired

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 10:58 AM
Post #5

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
Alright, came up with this, and it gives me the value of the cell in which was clicked, which is definately a step in the correct direction...

CODE

    Private Sub InventoryDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellDoubleClick
        Dim invId As Object = InventoryDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        inventoryIdLabel.Text = Convert.ToString(invId)
    End Sub


The only real problem now is that you can't depend on a user to click on the id field of the row, so you would need to obtain the id field of the row from what ever field was clicked in that row... any ideas?
User is offlineProfile CardPM
+Quote Post

Jayman

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 11:58 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,544



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

My Contributions
The rows and cells property of the datagridview takes an integer index as reference to a specific row or column in the grid. So determine which column is the one containing the ID field and replace e.ColumnIndex with that column number.

Don't forget the column and row numbers start from 0.

For example, if the ID column is the first column in the grid:
CODE

Dim invId As Object = InventoryDataGridView.Rows(e.RowIndex).Cells(0).Value

User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 12:32 PM
Post #7

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
You need to know the column that has the ID you're looking for (in my example its 0)

CODE

DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value


This sill return the first column in the row selected
User is offlineProfile CardPM
+Quote Post

webwired

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 12:46 PM
Post #8

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
Works like a charm, thank you very much... I think I pretty much have what I need now to complete my final project...

I can now select, insert, update, & delete sql with vb .net, I can pass values from a datagrid to another form, LOL, albeit I'm sure not in the manner in which an experienced coder would do it, but it works... I write the id I get from the datagrid to a text file, then on the load of the new form I pull that id from the text file and run the query...

Just wanted to thank you all again for your continued assistance.
User is offlineProfile CardPM
+Quote Post

m2s87

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 01:03 PM
Post #9

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 15 times
Dream Kudos: 1225
My Contributions
I am pretty sure that something like :
CODE
    Const index_of_id_row As Int32 = 762
    Private Sub DataGridView1_CellContentClick( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs _
    ) Handles DataGridView1.CellMouseDoubleClick

        If TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then
            Call SetValueToMyTextBox(CType(sender, DataGridView), e)
        End If
    End Sub
    Private Sub SetValueToMyTextBox( _
        ByVal sender As DataGridView, _
        ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs _
    )
        If sender.CurrentRow.Index = index_of_id_row Then
            inventoryIdLabel.Text = sender.CurrentCell.EditedFormattedValue()
        End If
    End Sub

would work.

Added:
Did not see the last post, when i posted this. Guess it's not bad because it also should show how to get rid of "late binding error".

This post has been edited by m2s87: 28 Aug, 2007 - 01:15 PM
User is offlineProfile CardPM
+Quote Post

webwired

RE: Obtaining Field Value From Datagrid On Double Click

28 Aug, 2007 - 01:27 PM
Post #10

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 5 times
Dream Kudos: 100
My Contributions
Thanks though m2s87.
User is offlineProfile CardPM
+Quote Post

aman bajwa

RE: Obtaining Field Value From Datagrid On Double Click

5 Sep, 2007 - 04:20 AM
Post #11

New D.I.C Head
*

Joined: 5 Sep, 2007
Posts: 1


My Contributions
Hi
u can try this:
for example u want Id Field on click of datagrid then try this i think this should work .

Dim invID AsInteger= DataGridView1.CurrentRow.Cells(<columnIndex of particular colum eg. 0 ,1 etc>).value

All d best.



QUOTE(webwired @ 28 Aug, 2007 - 06:09 AM) *

From what I'm finding, there doesn't appear to be any particular function available to obtain the value from a particular field from a double clicked on row of a datagrid...

So if you wanted to double click on a particular result in a datagrid, then pass one or more of the values of that row, you couldn't?

I was thinking something like this, but of course that doesn't work, any ideas?

CODE

    Private Sub InventoryDataGridView_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles InventoryDataGridView.CellContentDoubleClick
        Dim invID As String = InventoryDataGridView.CurrentRow(???ParticularField???)
    End Sub



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:03AM

Live VB.NET Help!

Be Social

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

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month