Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtain Primary Key from Row Selected in Datagrid 1

Status
Not open for further replies.

xpower

MIS
Oct 21, 2003
3
0
0
GB
Hi

I'm new to .net so please forgive these basic questions:

I have a form containing a datagrid, using the record selector, the user selects a row.

I need to be able to pass the PK from this selected row to a variable, which is used as a parameter for the next form.

Once I have updated the next form and closed it, I need to requery the Datagrid as additional records may have been added between events.

Must admit struggling with .net after years of MS Access Developemt.

Thanks in advance for help.

Xpower
 

Here's an example:

Code:
Private Sub grd_Click(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) _
                      Handles grd.Click
    Dim hit As System.Windows.Forms.DataGrid.HitTestInfo
    hit = grd.HitTest(grd.PointToClient(Cursor.Position))
    If hit.Type = DataGrid.HitTestType.RowHeader Then
        Dim frm As New Form2()
        frm.ShowDialog(Me)
        Try
            Me.LoadDataSet() 'The procedure used to fill the grid
        Catch eLoad As System.Exception
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
        End Try
    End If
End Sub

The thing is to open the second form as a modal dialog (frm.ShowDialog(Me)).
Using the overloaded ShowDialog method sets the modal owner to the first form, causing the code execution to pause until the modal form has been closed. This way the code that fills the datagrid is executed only after the second form is closed.

-Stephen Paszt
 
Jeez, sorry about the last post, I completely left out passing the PrimaryKey.

Here's the real example:

Code:
Private Sub grd_Click(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) _
                      Handles grd.Click
    Dim hit As System.Windows.Forms.DataGrid.HitTestInfo
    hit = grd.HitTest(grd.PointToClient(Cursor.Position))
    If hit.Type = DataGrid.HitTestType.RowHeader Then
        Dim iPkColumn As Integer = 0
        Dim frm As New Form2(grd.Item(grd.CurrentRowIndex, iPkColumn))
        frm.ShowDialog(Me)
        Try
            Me.LoadDataSet() 'The procedure used to fill the grid
        Catch eLoad As System.Exception
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
        End Try
    End If
End Sub

In the second form (Form2 in the above example), create an overloaded 'New' procedure so the form can receive the PrimaryKey:

Code:
Private m_PK As String
Public Sub New(ByVal strPK As String)
    m_PK = strPK
    InitializeComponent()
    'do something with the PK variable here
    'or in the load event handler
End Sub

I looked for a way to reference the PrimaryKey directly but could only find a PrimaryKey propery for a DataTable object. If anyone knows a way to reference the PrimaryKey of a DataRow, I'd like to see an example.

In the above example, you have to explicitly know which column is the PrimaryKey.
If you are filling the DataGrid with a select command similar to "SELECT ID, Name, Comments FROM tblComments" and ID is the PrimaryKey, the PrimaryKey's column index would be 0 since it is listed first in the SQL command (column index's are zero based). If you change the SQL command to "SELECT Name, ID, Comments FROM tblComments", the PrimaryKey's column index would be 1.
(i.e., the column's indices are dependent upon the order of the field name's in the SQL command)


 
Paszt

Thanks for your help, very much appreciated.

The ID is the first column in the grid, so the index is 0.

I'm sure I'll get the hang of it one day.

Mark

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top