DataGridView CellFormatting Prevents Form Formation

I use C #, Winforms and .Net 3.5

My form has its own DataGridView(with double buffering to prevent flickering during cell formatting events, as shown here ). When I search the database, I link the resulting dataset to datagridview.

I handle the event CellFormattingto draw strings of 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 fine, except that when I process CellFormatting, the drawing event of the whole form seems to be disabled. The cursor stops flashing in the text box, and the menu form looks like this:

Menu bar picture

, . , , , , . , , , .

resultsGridView.CellFormatting resultsGridView.CellFormatting datagridview .

- ?

+3
1

, . , , try catch .

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

, therow.Cells["Sealed"] . - therow.Cells["dataGridViewTextBoxColumn2"]. , DataPropertyName.

+1

Source: https://habr.com/ru/post/1724428/


All Articles