Question

I have a DataGrid, bound to database table customer.

I need to select the primary key ID value from database table of selected row in DataGrid.

How to do it ? please Help ..

Was it helpful?

Solution

You can add the ID to the select statement

string Query = "Select ID,Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New "; 

Since you want to hide the ID column, and your columns are generated automatically, register to the AutoGeneratingColumn event of your grid

datagrid_cindex.AutoGeneratingColumn += OnAutoGenetingColumns;

In OnAutoGenetingColumns, hide the ID column

private void OnAutoGenetingColumns(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Header.ToString() == "ID")
        e.Column.Visibility = System.Windows.Visibility.Collapsed;
}

When you want to retreive the ID of the selected row, use:

var selectedRow = datagrid_cindex.SelectedItem as DataRowView;
var id = selectedRow["ID"];

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top