17

I have a DataGridView. I set its .DataSource prop to be an BindingList of my own objects: a BindingList<IChessItem>

I then created some columns for it..

    DataGridViewTextBoxColumn descColumn = new DataGridViewTextBoxColumn();
    descColumn.DataPropertyName = "Description";
    descColumn.HeaderText = "Description";
    descColumn.Width = 300;

    DataGridViewTextBoxColumn gameIDColumn = new DataGridViewTextBoxColumn();
    gameIDColumn.DataPropertyName = "GameID";
    gameIDColumn.HeaderText = "Game ID";
    gameIDColumn.Width = 60;

    dataGrid.Columns.Add(descColumn);
    dataGrid.Columns.Add(gameIDColumn);

My question is.. I want to color one of the columns GREEN based upon data in another field of my BindingList). How can I do this?

I don't really have to show this field, I just want to act upon the data in it..

in my case, one of the fields of IChessItem shows whether the record is new, and I want to color the other fields in the datagridview to reflect that.

KevinDeus
  • 11,988
  • 20
  • 65
  • 97

2 Answers2

33

You can use the 'CellFormatting' event of the DataGridView. The DataGridViewCellFormattingEventArgs contains indexes of the row and the column of the current cell as it is being bound. I hope my code example makes some sense to you:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // Compare the column to the column you want to format
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
    {
        //get the IChessitem you are currently binding, using the index of the current row to access the datasource
        IChessItem item = sourceList[e.RowIndex];
        //check the condition
        if (item == condition)
        {
             e.CellStyle.BackColor = Color.Green;
        }
    }
}
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • interesting. This looks like it should work, unfortunately, this.dataGridView1.Columns[e.ColumnIndex].Name returns "" whenever it gets here. It looks like it *should* work tho. – KevinDeus Nov 02 '10 at 06:02
  • okok.. I got it to work by using the .DataPropertyName (since that was set up in my code above), and pointing it back to the datagrid item (as my sourcelist has been erased by this point..) IChessItem item = ((BindingList) this.dgvAvailableMoves.DataSource)[e.RowIndex]; – KevinDeus Nov 02 '10 at 06:14
  • 2
    A more robust approach is to compare directly to the column instead of checking the column name, i.e. `if (dataGridView.Columns[e.ColumnIndex] == dataGridViewColumnXXX) ...` – Simon MᶜKenzie Oct 31 '13 at 23:10
0

You may populate data in your DataGridView using any loop or datasource. Then for each DataGridViewRow in DataGridView1.Rows----

Chk the ref value ypu want to chk and then set DataGridviewCell[index].style.backColor property.

  • 6
    Please don't go this way for formatting the cells - if you have 10000 rows x 20 columns, you will be checking 200000 cells. On the other hand, if you will use the CellFormatting event, you are doing formatting only for visible cells (which is waaay faster). – zeroDivisible Mar 13 '12 at 11:25