Search code examples
c#wpfwpf-controlswpfdatagrid

Get Column value from DataGridRow in WPF


I'm creating a WPF application in which when a user clicks on a Row of the DataGrid, I need to take a Column value and using that value I need to get data from Database.

I'm able to Find the DataGridRow but unable to get the column values. Here is my code ...

DataGridRow BillRow = sender as DataGridRow;

I get the selected row details into BillRow (I'm able to see them in Visualiser) but unable to get the values into a variable. Can you help me ??


Solution

  • The following solution may be help you

     public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dataGrid, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    
                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
    
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
    
                return cell;
            }
    
            return null;
         }