Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract DataGridCell from DataGridCellinfo class in wpf

I want to know how to get the DataGridCell from DataGridCellInfo. Actually i have a some selected cells in datagrid, and SelectedCells property return DataGridCellInfo's collection, but i want to change the background of those cells at runtime too. so i need the datagrid cell.

kindly suggest me how to do so and also how to change the datagrid cell background color dynamically(through code) also.

Thanks

like image 508
manav inder Avatar asked Nov 08 '11 09:11

manav inder


2 Answers

To anyone who got here from a search engine, expecting to find an answer to the title in the question, look here: https://stackoverflow.com/a/17066695/937093

Content:

public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
    var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
    if (cellContent != null)
        return (DataGridCell) cellContent.Parent;

    return null;
}

edit

if you upvote this answer please don't forget to upvote the original answer I linked as well!

like image 112
Steffen Winkler Avatar answered Sep 17 '22 12:09

Steffen Winkler


To change the color of the cell dynamically this is the simplest way

cell.Background = new SolidColorBrush(Colors.Green);

and to get the datagrid cell, follow this link

WPF Datagrid: Programmatically editing a cell

Thanks to Natxo

like image 20
manav inder Avatar answered Sep 20 '22 12:09

manav inder