0

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 ..

Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • Hope this question is clear ! –  Oct 08 '13 at 06:44
  • Are you using `ORM` framework? Entity Framework? NHibernate? – Omri Btian Oct 08 '13 at 06:47
  • I am not using any framework until now.... i am simply making database(sqlite) driven application using wpf controls ... –  Oct 08 '13 at 06:57
  • Upload your code of how you bind grid to the database – Omri Btian Oct 08 '13 at 06:59
  • sqlitecon.Open(); string Query = "Select Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New "; SQLiteCommand createCommand = new SQLiteCommand(Query, sqlitecon); createCommand.ExecuteNonQuery(); SQLiteDataAdapter dataAdp = new SQLiteDataAdapter(createCommand); DataTable dt = new DataTable("Customer_New"); dataAdp.Fill(dt); datagrid_cindex.ItemsSource = dt.DefaultView; dataAdp.Update(dt); sqlitecon.Close(); –  Oct 08 '13 at 07:12

1 Answers1

1

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

Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • On a side-note I must say that your really should use some ORM framework. Binding the UI directly to the Database is a very tight coupling that you normally would want to avoid ... – Omri Btian Oct 08 '13 at 07:26
  • Thanks .....This is working 100%.....Can you share with me some good link of tutorial or Video tutorial for learning ORM framework from start to end .... –  Oct 08 '13 at 07:59
  • I use [NHibernate](http://www.codeproject.com/Articles/21122/NHibernate-Made-Simple) with [FluentNHibernate](http://www.fluentnhibernate.org/) configuration as [ORM](http://stackoverflow.com/questions/132676/which-orm-for-net-would-you-recommend). [This](http://www.d80.co.uk/post/2011/02/20/Linq-to-NHibernate-Tutorial.aspx) is a great tutorial to get you started. If my post answered your question, please mark it as _answered_. thank you :) – Omri Btian Oct 08 '13 at 08:09
  • 1
    This seems very clumsy considering most grids provide either an actual row property that is the PK, or a global property for the name of the row PK. – ProfK Feb 21 '15 at 13:35
  • @ProfK But wouldn't that be a case only if you are using `EF`, `ORM`, etc. It's not clear what data binding mechanism `OP` is using. – nam May 29 '20 at 03:41