Steveinbeloit

Hi
I have a datgridview which I created by dragging over a table from my DataSource tab. All works good.
I need to have a particular cell in each row colored based on the value of another column in that row. So the cell should be Red if the not-visable Status column relates to 90 days passed due.
Since I dragged it over, all the code to set it up was generated, I don't know how to have it jump in and color each cell accordingly.

How can I do this
Thanks,
Steve


Re: Windows Forms Data Controls and Databinding Change DataGridView Cell color based on other values in that row, VS 2005 VB .NET

Ken Tucker

I would use the CellFormatting event for this. Here is a simple example

http://www.vb-tips.com/dbpages.aspx ID=a426386e-6ecc-4126-a295-cf57ce269df2





Re: Windows Forms Data Controls and Databinding Change DataGridView Cell color based on other values in that row, VS 2005 VB .NET

timvw

If the conditions are heave it can be more efficient to subscribe to the BindingComplete event on the BindingSource (instead of the CellFormatting event which will be called multiple times)
And then iterate over all the rows (and their column values)

Code Snippet

for(int i = 0; i < this.dataGridView1.RowCount; ++i)
{
if (this.dataGridView[3, i].Value.ToString() == "Something")
{
this.dataGridView[4, i].Style.BackColor = Color.Red;
}
}








Re: Windows Forms Data Controls and Databinding Change DataGridView Cell color based on other values in that row, VS 2005 VB .NET

Steveinbeloit

Yes, these are just what I am after!

Thanks.