Solved

Primary Key value of selected DataGrid row

Posted on 2004-08-12
4
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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