Changing Cell Color according to IF condition in C# WPF source code

Asked

Viewed 239 times

2

I have a Datagrid in a form in C# WPF, as it is possible to execute a method recovers each Cell contained in that Datagrid and assign a possible value, or change the background color?

For example I have a Datagrid:

[ A ] [ D ] [ C ] [ C ]
[ B ] [ A ] [ B ] [ A ]
[ C ] [ A ] [ B ] [ D ]

And then I want to make a loop of repetition:

for (i<TamanhoDeLinhas) // Andar Linha
  for(j<TamanhodeColunas) // Andar Coluna
     if(DataGrid[i][j].Conteudo == A)
       DataGrid[i][j].BackGround = Color.Red;

It is possible to do this?

Or else, I have the corresponding values of I J, or I know in which position the values will be! I have to change the Cell background on line i=2 and j=3. How is this possible? Recovers Cell and assigns a new color according to its position or by reading one by one?

The Datagrid in question is linked to a Datatable and it generates columns and rows automatically, in case it is dynamic. then I do Datagrid.Sourceitems = Datatable.Defaultview ... And so I play all the items for the Datagrid.

Any of the solutions is valid.

2 answers

3


If you know the ratings, I took that test:

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

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  var cellInfo = new DataGridCellInfo(gd.Items[0], gd.Columns[2]);
  var cell = GetCell(cellInfo);
  cell.Background = Brushes.Red;
}

Understanding that in the window Loaded you have already assigned the values to your grid.

  • I have a Datatable, where it binds with Datagrid and it generates columns and rows automatically, in case it is dynamic. then I do Datagrid.Sourceitems = Datatable.Defaultview ... And so I play all the items for the Datagrid. When using this method you sent me appears the error message "The index was out of range. It should be non-negative and smaller than the collection size."

  • In the case my table in question is 4 by 4 and as in the example you sent is 0|2 would have to have worked

  • Add your question to the part of the code where you assign the grid dataContext or items, please. This message seems to occur pq Voce does not yet have the items in the grid.

  • right, but for example, I link the items to Grid and created a button called updates, so after the data appears on the grid I call the button click event updates, and yet this error appears!

  • Then, may I suggest that you do: Gd.Updatelayout(); after assigning the values. But in order to help, you’ll need to show the attribution code and the code that triggers the event to get the Cells.

  • I put the update in another location and it worked, thanks so much for the help! solved my problem.

Show 1 more comment

2

Create an extension. For example, DataGridExtensions.cs:

public static class DataGridExtensions
{
    public static DataGridCell GetCell(this DataGrid grid,  DataGridRow linha, int indiceColuna = 0)
    {
        if (linha == null) return null;

        var presenter = linha.FindVisualChild<DataGridCellsPresenter>();
        if (presenter == null) return null;

        var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(indiceColuna);
        if (cell != null) return cell;

        grid.ScrollIntoView(linha, grid.Columns[indiceColuna]);
        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(indiceColuna);

        return cell;
    }
}

Recover the cell:

var cell = meuDataGrid.GetCell(linha, indiceColuna);

Using the correct color type, both cases will work. WPF Background is the type System.Windows.Media.Brush.

Examples of use with recovered cell:

// using System.Windows.Media;

cell.Background = Brushes.White;
cell.Background = new SolidColorBrush(Color.White);
cell.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
cell.Background = System.Windows.SystemColors.MenuHighlightBrush;
  • This will change the color of the whole Datagrid I want to change the background of some cells only

  • @Brunosilva updated the answer

  • Thank you @mercador I will understand and implement it now! but apparently is giving error when executing this part "Findvisualchild<Datagridcellspresenter>"

  • @Brunosilva see this answer https://stackoverflow.com/a/25229554/3551913

  • Thank you for your attention, you’ve helped a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.