I have to access the value that underlies the active cell of a DataGrid (The cell with the black border around it).
Luckily DataGrid has a lot of properties such as CurrentCell, CurrentItem SelectedCells SelectedItem and SelectedItems seeming to provide me with the data I want.
However I have not figured out how to access the cell in an easy way. I also have changed the …
SelectionMode="Single" SelectionUnit="Cell"
...properties but in the end I had to do something like this:
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if(null != cellInfo && cellInfo.IsValid){
object[] array = cellInfo.Item as object[];
if (null != array &&
cellInfo.Column.DisplayIndex >= 0 &&
cellInfo.Column.DisplayIndex < array.Length) {
object cellValue=array[cellInfo.Column.DisplayIndex];
if (null != cellValue) {
// Here we are
}
}
}
In my example, the row is built through an object-array containing various object-types. I'm aware that I could execute the binding on the cellInfo.Column (after a cast), however that is not the point. My question is, if I make something really wrong, because I cannot imaging that such a powerful piece of software as DataGrid is, can not provide me the desired value without doing such a lot of coding.
What have I missed or is it really such complicated to get the current cells value.
UPDATE
As Quartermeister explained in his very good answer, there is not a single property to access the cells value and this is has a meaningfull cause. besides, beware of using the DisplayIndex as I do in my example if you let the user rearrange the columns.