Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 95,475 VB.NET Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 956 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert

Register to Make This Box Go Away!


Obtaining field value from datagrid on double click

 
Reply to this topicStart new topic

Obtaining field value from datagrid on double click

webwired
post 28 Aug, 2007 - 06:09 AM
Post #1


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 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


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page


m2s87
post 28 Aug, 2007 - 07:32 AM
Post #2


D.I.C Regular

Group Icon
Joined: 28 Nov, 2006
Posts: 390



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.


--------------------
IPB Image
IPB Image
IPB Image
Then anyone who leaves behind him a written manual, and likewise anyone who receives it, in the belief that such writing will be clear and certain, must be exceedingly simple-minded. (Plato, Phaedrus)

IPB Image
User is offlineProfile CardPM

Go to the top of the page

webwired
post 28 Aug, 2007 - 07:57 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 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 - 08:25 AM


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page

webwired
post 28 Aug, 2007 - 11:06 AM
Post #4


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 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 - 11:23 AM


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page

webwired
post 28 Aug, 2007 - 11:58 AM
Post #5


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 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?


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 28 Aug, 2007 - 12:58 PM
Post #6


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 5,966



Thanked 10 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

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

Go to the top of the page

PsychoCoder
post 28 Aug, 2007 - 01:32 PM
Post #7


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,329



Thanked 19 times

Dream Kudos: 7050

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

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


--------------------
IPB Image
Facebook | My Blog | My Site | Forums | Rules | Twitter
Syntax_Terror => Friend and fellow code-a-holic. May you rest in piece my friend
User is offlineProfile CardPM

Go to the top of the page

webwired
post 28 Aug, 2007 - 01:46 PM
Post #8


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 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.


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page

m2s87
post 28 Aug, 2007 - 02:03 PM
Post #9


D.I.C Regular

Group Icon
Joined: 28 Nov, 2006
Posts: 390



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 - 02:15 PM


--------------------
IPB Image
IPB Image
IPB Image
Then anyone who leaves behind him a written manual, and likewise anyone who receives it, in the belief that such writing will be clear and certain, must be exceedingly simple-minded. (Plato, Phaedrus)

IPB Image
User is offlineProfile CardPM

Go to the top of the page

webwired
post 28 Aug, 2007 - 02:27 PM
Post #10


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 63



Thanked 1 times

Dream Kudos: 100
My Contributions


Thanks though m2s87.


--------------------
Complete and utter newbie.
User is offlineProfile CardPM

Go to the top of the page

aman bajwa
post 5 Sep, 2007 - 05: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

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/5/08 03:39AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->