Solved

Primary Key value of selected DataGrid row

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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