As WPF DataGrid
is more flexible than a WinForms DataGridView
, gettings values seems difficult. So I made the below static
extension methods/funcitons to get SelectedRow
, SelectedCell
& SelectedCellValue
. Credits to some post I saw a while back in SO for GetVisualChild
and some of the functions given below.
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
To get SelectedRow
:
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
Gets the DataGridRow
when you know the RowIndex
:
public static DataGridRow GetRow(this DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
Gets the DataGridCell
when you have a DataGridRow
and a columnIndex
:
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int columnIndex)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[columnIndex]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
return cell;
}
return null;
}
Gets the DataGridCell
when you have a DataGridRow
and a columnName
:
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, string columnName)
{
int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
return GetCell(grid, row, index);
}
Gets the DataGridCell
when you have a rowIndex
and a columnIndex
:
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
DataGridRow rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
Gets the DataGridCellValue
when you have a DataGridRow
and a ColumnName
:
public static string GetCellValue(this DataGrid grid, DataGridRow row, string columnName)
{
int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
DataGridCell dgc = GetCell(grid, row, index);
string str = Convert.ToString(((TextBlock)dgc.Content).Text);
return str;
}
Gets the DataGridCellValue
when you have a rowIndex
and a ColumnName
:
public static string GetCellValue(this DataGrid grid, int rowIndex, string columnName)
{
DataGridRow row = grid.GetRow(rowIndex);
int columnIndex = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
DataGridCell dgc = GetCell(grid, row, columnIndex);
string str = Convert.ToString((TextBlock)dgc.Content);
return str;
}