How to determine the selected cell value in a DataGrid? (WPF)

How to determine SelectedCell Value in a DataGrid ? (WPF)

My DataGrid has 9 columns and 5 rows, and I want to know the Value clicked row [0] Value .

I used this code in Windows Form:

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { var a = dataGridView1[e.ColumnIndex, e.RowIndex].Value; } 

but I do not know the equivalent code in wpf.

+4
wpf
01 Oct '10 at 9:20
source share
5 answers

You should use the DataGrid_SelectedCellsChanged event.

  private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { foreach (var item in e.AddedCells) { var col = item.Column as DataGridColumn; var fc = col.GetCellContent(item.Item); if (fc is CheckBox) { Debug.WriteLine("Values" + (fc as CheckBox).IsChecked); } else if(fc is TextBlock) { Debug.WriteLine("Values" + (fc as TextBlock).Text); } //// Like this for all available types of cells } } 

NTN

+3
01 Oct '10 at 12:52
source share

The definition of the selected cell value is more relevant to the WinForms object. WPF is designed to work in different ways; your user interface should be separate from the logic. Thus, DataGrid becomes a tool for presentation, and not something that you need to poke and push towards values.

Instead, with WPF, you want to deal with the objects that you have attached to the grid itself, regardless of how they are displayed. Forget about the grid - just find the object that is currently "selected" by the user from the list of related objects.

SelectedItem is a property of the grid itself and, thanks to the excellent WPF binding mechanisms, you can bind this value to the ViewModel property via XAML:

 ItemsSource="{Binding Orders, Mode=OneWay}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" 

When the user selects an item in the grid, the two-way binding will update the SelectedItem property in the ViewModel with the value of this object in this row.

This way, you don’t even have to deal with knowledge of the grid or user interface.

Hope this makes sense. I know this is a different approach and a different way of thinking coming from WinForms.

+3
01 Oct '10 at
source share

I found a solution posted by others in another thread on stackoverflow: WPF Toolkit DataGrid SelectionChanged Getting the value of a cellular network

Give it a try.

+1
Jan 26 2018-12-12T00:
source share
  private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { var item = e.AddedCells[0]; { var col = item.Column as DataGridColumn; var fc = col.GetCellContent(item.Item); if (fc is CheckBox) { } else if (fc is TextBlock && col.DisplayIndex == 0) { textBlock1.Text = (fc as TextBlock).Text; } } } 
0
Oct 02 '10 at 17:25
source share

sometimes binding to SelectedItem does not work (depends on how crazy your model should be. I need to transpose the model, so everything is upside down and normal defaults do not work all the time. given that, in dataGrid selectedCellChanged you can access the bound object:

based on the previous example Orders [], where each Order will have an array of SubOrders

 foreach (var selectedCell in e.AddedCells) { var order = (Order)selectedCell.Item; var subOrder = order.SubOrders[selectedCell.Column.DisplayIndex-1]; var someValue = subOrder.Value; 

}

0
May 4 '11 at
source share



All Articles