使用WPF获取DataGrid中所选行项的主键值

时间:2013-10-08 06:43:23

标签: wpf wpfdatagrid advanceddatagrid dojox.grid.datagrid

我有DataGrid,绑定到数据库表customer

我需要从DataGrid中所选行的数据库表中选择主键ID

怎么做?请帮助..

1 个答案:

答案 0 :(得分:1)

您可以将ID添加到选择语句

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

由于您要隐藏ID列,并且您的列是自动生成的,请注册到您网格的AutoGeneratingColumn事件

datagrid_cindex.AutoGeneratingColumn += OnAutoGenetingColumns;

OnAutoGenetingColumns中,隐藏ID列

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

如果要检索所选行的ID,请使用:

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

希望这有帮助