im not sure if you want to simply change the color of the cell in a row that has a matching profolio id of the approved portfolio's, but it should probably done a bit differently. Currently your code requires # of items in the DataGridView * # of items in the DataSet. Whats worse is that you are trapping the CellFormatting handler which is fired each time a row needs to be repainted.
Quick Solution:
Replace
Code:
e.Value = Properties.Resources.LightGreen;
with
Code:
try{
myDataGridView.Rows[iRow].Cells["MessageColumn"].Style.BackColor = Color.LightGreen;
} catch (Exception e){ Console.WriteLine(e.Message); }
Long Solution:
Rewrite this to trap the DataSourceChanged or CellValueChanged depending on other factors of your app. When filling your dataset use adapter.FillSchema(ds) first to gather any primary keys and metadata. Assuming you have primary keys defined, you can query the dataset for specific items using (Assuming your DataSet is named ds)
Code:
ds.Tables[0].Rows.Find({PrimaryKey})
or
Code:
ds.Tables[0].Select ("Portfolio=" + SelectedID); //Returns a DataRow[] array but in your case should only have 1 row
then you can simply say
Code:
if(ds.Tables[0].Select ("Portfolio=" + SelectedID) != null){
//then a match was found; mark this row
myDataGridView.Rows[iRow].Cells["MessageColumn"].Style.BackColor = Color.LightGreen;
}