Question Validate selected column in datagridview

yazink

New member
Joined
Mar 6, 2010
Messages
3
Programming Experience
Beginner
I'm trying to validate that a particular column in a datagridview has been selected.

I've created this code which transfers the value of the selected Customer ID cell in the datagridview to another form.

Private Sub mnuFileEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileEdit.Click

'Define variable for the current selected cell value
strSelectedCustomerID = dgvCustomerSearch.CurrentCell.Value.ToString()

'Define the new form and transfer the selected customer ID value to it
Dim customerEditForm As New frmCustomerEdit
customerEditForm.lblCustomerPK.Text = strSelectedCustomerID
customerEditForm.ShowDialog()

It's working fine but at the moment any column in the datagridview can be selected and transferred which causes an error.

So I want to create an If Then statement which confirms that the selected column with the dgv is the PK_ID (customer ID) column before processing the rest of the code. I've tried playing around with "If dgvCustomerSearch.SelectedColumns("PK_ID") Then" but a "Value of type...cannot be converted to Integer" error occurs.

I'd appreciate if you could let me know the correct coding.

Thanks, Kevin
 
Ah ok. I had picked up the reference to CurrentCell property from searching the net - am new to VB and still finding my way around.

I needed to grab the value in the first column in the dgv so I've changed the code to strSelectedCustomerID = dgvCustomerSearch.CurrentRow.Cells(0).Value.ToString(), and it's working great.

Thanks for the response and for pointing me in the right direction.
 
Back
Top