WPF Datagrid Get Selected Cell Value

前端 未结 12 2091
遇见更好的自我
遇见更好的自我 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:47

    Ok after doing reverse engineering and a little pixie dust of reflection, one can do this operation on SelectedCells (at any point) to get all (regardless of selected on one row or many rows) the data from one to many selected cells:

    MessageBox.Show(
    
    string.Join(", ", myGrid.SelectedCells
                            .Select(cl => cl.Item.GetType()
                                                 .GetProperty(cl.Column.SortMemberPath)
                                                 .GetValue(cl.Item, null)))
    
                   );
    

    I tried this on text (string) fields only though a DateTime field should return a value the initiate ToString(). Also note that SortMemberPath is not the same as Header so that should always provide the proper property to reflect off of.

    
    

提交回复
热议问题