Solved

Primary Key value of selected DataGrid row

Posted on 2004-08-12
4
646 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now