Solved

Primary Key value of selected DataGrid row

Posted on 2004-08-12
4
656 Views
Last Modified: 2006-11-17
Please help me.

I have to get Primary Key value when user selects row in datagrid. I know that I can use CurrencyManager.Current to get selected row but how to get Primary Key from that?

Patrik
0
Comment
Question by:patrikt
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
ID: 11782099
I assume you use a dataview as your datasource for the datagrid

        dv.Item(DataGrid1.CurrentRowIndex).Item("yourprimarykey")

if you use a datatable


        dt.Rows(DataGrid1.CurrentRowIndex).Item("yourprimarykey")


0
 
LVL 12

Author Comment

by:patrikt
ID: 11782457
This not solve my problem.

1) primary key is not displayed in grid
2) I don't know which column is primary key. It is contained in dataset definition and I don't want to code that.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11782637
1) it doesn't matter if the primary key isn't displayed in the grid, since in my comment I used the underlaying table.
2) you can get the information about the primary key like this (I assume you know which table is used)

        Dim x As DataColumn
        For Each x In ds.Tables(0).PrimaryKey
             ds.tables(0).rows(DataGrid1.CurrentRowIndex).Item(x.columnname)
        Next



0
 
LVL 12

Author Comment

by:patrikt
ID: 12011607
I'll accept answer but it is not fully correct.

You have to refer to SelectedRow rather then ordinal possition of row in dataGrid. If DataGrid will be sorted it can have row numbers not matching table row numbers.

///dv.Item(DataGrid1.CurrentRowIndex)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 95
Visual Studio 2013 Shortcut (VB) 4 46
Variable Event ? 3 27
SQL LINE CONTINUATION ISSUE 12 33
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question