WPF Datagrid Get Selected Cell Value

前端 未结 12 2087
遇见更好的自我
遇见更好的自我 2020-11-30 08:47

I want to get value for selected cell in datagrid , please anyone tell how to do this. i used SelectedCell changed event , how can i do that?

dataGrid1.Curre         


        
12条回答
  •  心在旅途
    2020-11-30 09:26

    //Xaml Code
    
    
    
    
    
    //C# Code
     DataRowView row = (DataRowView)grid1.SelectedItem;
     MessageBox.Show(row["Prescription"].toString() + " " + row["Date"].toString());
    

    As WPF provides binding in DataGrids, this should be rather transparent. However, the following method only works, if you have used SQLDataAdapter and provided a binding path to your DataGridColoumns. For eg. Let's say the above datagrid is named grid1, which has auto generate columns set to false, and is using binding to bind column names to Headers. In this case, we use the 'row' variable of type 'DataRowView' and store the selected row in it. Now, use your Binding Paths, and reference individual columns of the selected row. Hope this helps! Cheers!

    PS: Works if SelectionUnit = 'Row'

提交回复
热议问题