garethh86
asked on
Code behind for Button Click in Datagridview Windows App
Hey there.
This is for a windows App made in VS2008.
I've added a button column in a datagridview, when clicked I would like to get the ID of the record selected. The ID of the record is the second column along, next to the select button. Been playing around with findcontrol but I'm not getting very far.
Thanks
This is for a windows App made in VS2008.
I've added a button column in a datagridview, when clicked I would like to get the ID of the record selected. The ID of the record is the second column along, next to the select button. Been playing around with findcontrol but I'm not getting very far.
Thanks
BTW...Ignore the first part about DataKeyNames. I started thinking about GridView with web stuff, not the DataGridView. Sorry about that if it was confusing....I just went the wrong way. You can just keep the DataGridView the way it is and use this.
Shawn
Shawn
ASKER
How do I access the button though if it is made at runtime? I need code for finding that control, sorry, my fault, didn't make that clear enough in the question.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Sorry, I'm doing this is VB.net, coverted to code below but 'column1' is coming up as not declared, would you know what it should be changed to for this to work in vb.net? Thanks for your help so far!
Private Sub dataGridView1_CellContentC lick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
' find out which column was clicked
If DataGridView1.Columns(e.Co lumnIndex) = Column1 Then
Dim strMessage As String = DataGridView1.Rows(e.RowIn dex).Cells (2).Value. ToString()
MessageBox.Show(strMessage )
End If
End Sub
Private Sub dataGridView1_CellContentC
' find out which column was clicked
If DataGridView1.Columns(e.Co
Dim strMessage As String = DataGridView1.Rows(e.RowIn
MessageBox.Show(strMessage
End If
End Sub
ASKER
Got it, https://www.experts-exchange.com/questions/23373255/VB-net-2-0-Windows-Forms-DataGridView-Get-Cell-Value-from-Column-Name-and-Row.html
See code below, I'll award points for putting me on the right track, thanks!
See code below, I'll award points for putting me on the right track, thanks!
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim strTest As String
strTest = Me.DataGridView1.CurrentRow.Cells("IDDataGridViewTextBoxColumn").Value
Label6.Text = strTest
End Sub
ASKER
Came back to this to refresh my memory, in case anyone else comes across this and is stuck heres the vb.net code for determining which column was clicked:
Private Sub SearchResultsDataGrid_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles SearchResultsDataGrid.CellContentClick
If SearchResultsDataGrid.Columns(e.ColumnIndex).Equals(SearchResultsDataGrid.Columns(0)) Then
'Insert your code here, assumes that your button control is in the first column.
End If
dim myID as string
myID = DataGridView1.Rows(e.rowin
Shawn