views:

623

answers:

1

I am using C#, Winforms, and .Net 3.5

My form has a custom DataGridView (double-buffered to prevent flickering during my cellformatting events, as seen here). When I perform a database search, I bind the resulting dataset to the datagridview.

I handle the CellFormatting event to paint rows a certain color, depending on their data.

My DataGridView code:

resultsGridView.DataSource = results.DefaultViewManager.DataSet.Tables[0];
resultsGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue;
resultsGridView.BorderStyle = BorderStyle.Fixed3D;
resultsGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(resultsGridView_CellFormatting);

My CellFormatting code:

void resultsGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    int rowIndex = e.RowIndex;
    DataGridViewRow therow = resultsGridView.Rows[rowIndex];
    if ((bool)therow.Cells["Sealed"].Value == true)
    {
     therow.DefaultCellStyle.BackColor = Color.Pink;
    }
    if (therow.Cells["Database"].Value as string == "PNG")
    {
     therow.DefaultCellStyle.BackColor = Color.LightGreen;
    }
}

Everything works great except that, when I handle the CellFormatting, the whole form's Paint event seems to be turned off. The cursor stops blinking in the textbox, and the form's menustrip looks like this:

Menu bar picture

The top is before a search, the bottom after. The menubar won't redraw until I mouse over where the menuitems are, and then the last item to be highlighted will stay that way when I move the mouse out of the menubar. Moving the form seems to cause it to repaint, but then the problem remains.

Commenting out the resultsGridView.CellFormatting line in the datagridview code completely fixes the problem.

Am I painting the cells wrong, or is there something else I need to handle?

+1  A: 

You are probably causing an exception inside this event. I'm not sure how the handling is defined, but surrounding the code with a try catch would be a first step.

try 
{
   int rowIndex = e.RowIndex;
   ....   
}
catch(Exception ex)
{
    System.Diagnostics.Trace.Error(ex.message);
}

On a second look, I don't think therow.Cells["Sealed"] will work. Try something like therow.Cells["dataGridViewTextBoxColumn2"]. Cells is indexed by Column Name, not DataPropertyName.

Henk Holterman
The "Sealed" part is not the data property, but rather a bool condition in my database. The program I'm working on does a search on a name and can show whether our case file in the department has been sealed or not.
Jared Harley
No exceptions to catch. I added `Systems.Diagnotics.Trace.WriteLine("start")` at the beginning of the formatting event, and it's running a LOT, even after the cells have been displayed and nothing else is going on.
Jared Harley
But is "Sealed" the name of a Dgv Column object?
Henk Holterman
And I think you should set `e.CellStyle.BackColor`, not `theRow.DefaultCellStyle.BackColor`.
Henk Holterman
You got it - changing how the cells were being painted was the trick. Changing to `e.CellStyle.BackColor` fixes it.
Jared Harley
And yes, "sealed" is the name of the column in the database, and in the datagridview (which is bound)
Jared Harley
Maybe you should read the part about Sealed again - the database name is irrelevant here, it is about the component.
Henk Holterman