I can not comment directly, so I checked the answer and voted to show the idea
public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid) { var itemsSource = grid.ItemsSource as IEnumerable; if (null == itemsSource) yield return null; foreach (var item in itemsSource) { var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (null != row) yield return row; } } private void DataGrid_Details_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { var row_list = GetDataGridRows(DataGrid_Details); foreach (DataGridRow single_row in row_lis) { if (single_row.IsSelected == true) { MessageBox.Show("the row no."+single_row .GetIndex ().ToString ()+ " is selected!"); } } } catch { } }
But what happens is that while I scroll, the selected line repeatedly shows up, I think due to the approach of ContainerFromItem .
Is it possible to change the background of a DataGrid programmatically, only in C # code?
I tried to play a little with the selected index, and it is not as simple as in WindowsForms . So, I tried to figure out how to perform the selection.
I use: DataTable in a DataObject which is the source of the dataObject.DataTable of the DataGrid elements is dataObject.DataTable . I know that I need to execute .Background = Brushes."DesiredColor" in a DataGridRow. But am not sure if there is any relation between DataGridRow. But am not sure if there is any relation between DataGridRow. But am not sure if there is any relation between DataGridRow. But am not sure if there is any relation between selected index/selected item/ DataGrid selected index/selected item/ SelectedItems' and the Background property.
naschd Jun 04 '19 at 11:43 on 2019-06-04 11:43
source share