I've a Datagrid on my form and I need to know the value from the RecordID field regarding a particular cell that I've clicked. In other words, lets say I have a datagrid showing fields Name, Address, City and when I click on the cell regarding address I need to get the ClientID field from the database. How can I get that
Thanks,
Joao Pinto

Determine the ID of a record from a Datagrid
pablog
Using your code I receive an error message: Index was out of range. Must be non-negative and less than the size of the collection.
Here is the code that I've:
Private
Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Dim Row As DataGridViewRow = DataGridView1.SelectedRows(0) Dim CLientId As String = CInt(Row.Cells("ID_Ocorrencia").Value) If DataGridView1.CurrentCell.ColumnIndex = 7 ThenMsgBox(CLientId)
End If End SubCan you please tell me what I'm doing wrong
thanks,
Joao Pinto
carol chen
Whether or not you choose to display the PrimaryKey(CLientID) your Bound Data should include the PrimaryKey so that you don't have to go all the way back to the database to get the key...it will live with the record even though you are not displaying it....
Dim
Row As DataGridViewRow = dg.SelectedRows(0) Dim CLientId As Integer = CInt(Row.Cells("CLientId").Value)Blipwort
I still get the same error message with the code bellow:
Private
Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Dim Row As DataGridViewRow = DataGridView1.SelectedRows(0) Dim CLientId As String = CInt(Row.Cells("ID_Ocorrencia").Value) If DataGridView1.CurrentCell.ColumnIndex = 6 ThenMsgBox(CLientId)
End If End SubThe error message appears on the row Dim Row As DataGridViewRow = DataGridView1.SelectedRows(0)
What can be the problem
Thanks,
Joao Pinto
Tomasz K.
Remember that the collections are 0 based and not 1 based so this is looking at the 8th column in the grid.
This could account for you index out of range - as if your grid was on 7 columns wide - looking at columnindex 7 would be out of range.
Ramazan Acar
Have you determined that selectedrows actually contains any items otherwise selectedrows(0) would generate this error.
You need to check the selectedrows.count to determine that there is at least 1 record for this to work.
dandrievsky
I've managed for it to work! Here is the code:
Private
Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick If DataGridView1.CurrentCell.ColumnIndex = 7 Then Dim Row As DataGridViewRow = DataGridView1.CurrentRow Dim CLientId As String = Row.Cells("ID_Ocorrencia").ValueMsgBox(CLientId)
End If End SubI've changed the SelectedRows(0) for the CurrentRow and it works fine!
Thanks for all your help.
Joao Pinto
Chas2002
If your using a datagridview and VB Express/2005 then you can obtain the ID cell from the dataset which the grid is bound to - even if the datagridview doesnt contain a column for this item by using the databounditem property which allows you to obtain the values from the underlying dataset being bound to a datagrid.
I used this in a similar circumstance where I didnt want to create a ID column on the grid but required it for updating existing records as the ID was the unique index for the table.
Datagridview DataboundItem Property
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.databounditem.aspx