How to freeze the datagridview to return the current cell value only.

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
I have the following code, When i click on any cell, of the grid of any column, it return the value of that particular cell in admissionhead.txtauin.Text textbox.

But i want it should be like - any cell of that particular row is clicked, only uin field should be assigned to admissionhead.txtauin.Text

Presently it returns the value of any cell tht is clicked.


Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then 'AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell
            admissionhead.txtauin.Text = sender.CurrentCell.EditedFormattedValue().ToString().Trim()
            Me.Hide()
        End If
    End Sub

    Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell Then 'AndAlso TypeOf sender.CurrentCell Is DataGridViewTextBoxCell
            admissionhead.txtauin.Text = sender.CurrentCell.EditedFormattedValue().ToString().Trim()
            Me.Hide()
        End If
    End Sub

    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
            admissionhead.txtauin.Text = sender.CurrentCell.EditedFormattedValue().ToString().Trim()
            Me.Hide()
        End If
    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
what is the column index of the uin? If it is in column 0 (the first column, then simply add this to your cellcontentclick event
admissionhead.txtauin.Text = CType(sender, DataGridView).Item(0, e.RowIndex).Value

Open in new window

Author

Commented:
The column index is 1, but after implementing its returning False

admissionhead.txtauin.Text = admissionhead.txtauin.Text = CType(sender, DataGridView).Item(1, e.RowIndex).Value

Commented:
False as in the word false? Apply the ToString to the value

Author

Commented:
Yes False is coming in the textbox. To string is also returning false

Commented:
Try this in the DataGridView's CellContentClick event.
admissionhead.txtauin.Text = CType(sender, DataGridView).Item(1, e.RowIndex).FormattedValue

Open in new window

NOT
admissionhead.txtauin.Text = admissionhead.txtauin.Text = CType(sender, DataGridView).Item(1, e.RowIndex).Value

Open in new window

The code you have will always give a Boolean result since you have two equals' signs! (and that is NOT the code I suggested before).

Author

Commented:
sorry it didnt worked out. However i applied my own logic.Thanks

Author

Commented:
ok

Commented:
You need to be honest with your remarks, and there is nothing wrong with admitting that you made an error!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial