Selecting Row ID in datagrid (1 Viewer)

skea

Registered User.
Local time
Today, 12:03
Joined
Dec 21, 2004
Messages
342
I use this function to select the current record ID in my datagrid. It does it well only when you have used/clicked the Row Header of the datagrid.
When some one clicks from cell to cell, the rows can change too.
I need a function which selects the current Record ID no matter whether the user has clicked in the data grid cell or on the Row Header.
Does any one here have an idea of how this can be done, or how the function below can be used to accomplish this, or has any one used CurrentRowIndex in this situation?

Code:
Sub ShowRecordID()
        Dim myConn As New SqlConnection(strConn)
        Dim strSQL As String = "SELECT * FROM tblStudentDetails"
        Dim daDetails As New SqlDataAdapter(strSQL, strConn)
        Dim dsDetails As New DataSet

        myConn.Open()
        Dim pt As System.Drawing.Point = Me.dgNewStudents.PointToClient(Cursor.Position)
        Dim Hti As DataGrid.HitTestInfo = Me.dgNewStudents.HitTest(pt)
        If Hti.Type = DataGrid.HitTestType.RowHeader Then
            daDetails.Fill(dsDetails, "tblStudentDetails")
            Me.tbStreamID.Text = dsDetails.Tables(0).Rows(Hti.Row).Item("StudentID")
        End If
        myConn.Close()

    End Sub

Edits: I realised that one can be able to disable the datagrid cells and do away with the above problem.
The solution to get out of this cave must be better than this, i suppose.
Actually i have found this method hard to deal with, since when the sorting at the column headers is it self not bound to the datasource of the datagrid. It gives me a different value if these column headers are clicked for sorting.
 
Last edited:

skea

Registered User.
Local time
Today, 12:03
Joined
Dec 21, 2004
Messages
342
Thanks WindSailer. I have looked at that site before, but there was nothing of this nature.

Guess what! i got it, and when i did i swang on my chair many many times...:D
I deeply studdied one of the tutorials i scrapped from some where, i think from a microsoft geek!, i suppose.
Here is what i did:
I declared and bound my datagrid to a DataView as its DataSource, then i declared a string variable to get the ID which i assigned to the datagrid's currentRowIndex property. The currentRowIndex property is some how associated with the dataView property.
 
Last edited:

Users who are viewing this thread

Top Bottom