I have this problem: in a windows forms C# app I have a normal form with a DataGridView with CellFormatting event (I need to format 5-6 columns; with 1 everything is ok).
The problem is that the form is not completely loaded/rendered (some elements don't get the right color, the labels are incomplete). The dataGridView (with 2 rows) also has some problems (when I click on a row that has a cell formatted with a backcolor, this color goes on the other row).
I have enabled the DoubleBuffering (the solution with the new class DBDataGridView available here: How to prevent DataGridView from flickering when scrolling horizontally?) but it still does not work.
I have noticed that among the properties of the dataGridView are these 2: -DoubleBuffered: true -DoubleBufferedEnabled: false
Could be this the problem ? How can I set to true the DoubleBufferedEnabled ?
I am using Visual Studio 2013, version 12.0.3 update 4
These are the dataGridView properties:
Thanks
UPDATE here because in the comments it is not possible to attach images:
yes, my app is winforms. this is the code that works (the "cell" is defined outside as "private DataGridViewCell cell = null;"). Some columns are hidden (like "accettato") and I use them to format other columns.
private void dataGridPrevW_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == dataGridPrevW.Columns["costo"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["acconti"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["complDaIncassare"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["costoLavEseg"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldoFinePr"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldolaveseg"].Index
) && e.Value != null)
{
e.CellStyle.Format = "N02";
}
if ((e.ColumnIndex == dataGridPrevW.Columns["costo"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["acconti"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["complDaIncassare"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["costoLavEseg"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldoFinePr"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldolaveseg"].Index
) && (e.Value == null || e.Value.ToString().Trim().Equals("")))
{
e.CellStyle.Format = "";
}
cell = dataGridPrevW.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (e.ColumnIndex == dataGridPrevW.Columns["acconti"].Index)
{
cell.ToolTipText = dataGridPrevW.Rows[e.RowIndex].Cells["stringaAcconti"].Value.ToString();
}
/*if (e.ColumnIndex == dataGridPrevW.Columns["descrizione"].Index)
{
if (dataGridPrevW.Rows[e.RowIndex].Cells["accettato"].Value != null && dataGridPrevW.Rows[e.RowIndex].Cells["accettato"].Value.ToString().Equals("1"))
{
cell.Style.BackColor = Color.PaleGreen;
cell.ToolTipText = "Accettato";
}
if (dataGridPrevW.Rows[e.RowIndex].Cells["chiuso"].Value != null && dataGridPrevW.Rows[e.RowIndex].Cells["chiuso"].Value.ToString().Equals("1"))
{
cell.Style.BackColor = Color.Moccasin;
cell.ToolTipText = "Chiuso";
}
if (dataGridPrevW.Rows[e.RowIndex].Cells["chiusobn"].Value != null && dataGridPrevW.Rows[e.RowIndex].Cells["chiusobn"].Value.ToString().Equals("1"))
{
cell.Style.BackColor = Color.Gainsboro;
}
if (dataGridPrevW.Rows[e.RowIndex].Cells["chiusonb"].Value != null && dataGridPrevW.Rows[e.RowIndex].Cells["chiusonb"].Value.ToString().Equals("1"))
{
cell.Style.BackColor = Color.DarkGray;
}
} */
}
and this is the result (OK with the commented code):
and this is the result (KO) without the comments, I mean the whole uncommented code:
As you can see, in the top the elements are not completely loaded (there are the green parts and the "plus" button appears 2 times, the title is different - "Elenco cartelle per paziente" instead of "Cartella paziente" that is the title of the previous form). The three rows are ok, formatted by the uncommented lines (I think there is a problem here, too, because if I click the green row, the green color disappears and it appears again if I click on another row).
UPDATE: it seems the problem is on the form itself because the top elements are not in the dataGridView...I also tried to set DoubleBuffered = true on the form but I got the same behavior. Or maybe because the DataGridView is not doubble buffered then the form is not completely loaded ??
UPDATE: the problem with the green color that dissapears when click on that row is "normal"...it means that the setting AlternatingRowColor overrides the color that the cell already has (set before in CellFormating or elsewhere). I have solved it this way:
private void dataGridPrevW_SelectionChanged(object sender, EventArgs e)
{
for (int counter = 0; counter < (dataGridPrevW.Rows.Count); counter++)
{
if (dataGridPrevW.Rows[counter].Cells["descrizione"].Style.BackColor == Color.PaleGreen)
{
dataGridPrevW.Rows[counter].Cells["descrizione"].Style.SelectionBackColor = Color.PaleGreen;
}
}
}
Here is the code for DataBindingComplete:
private void dataGridPrevW_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int counter = 0; counter < (dataGridPrevW.Rows.Count); counter++)
{
if (dataGridPrevW.Rows[counter].Cells["accettato"].Value != null && dataGridPrevW.Rows[counter].Cells["accettato"].Value.ToString().Equals("1"))
{
dataGridPrevW.Rows[counter].Cells["descrizione"].Style.BackColor = Color.PaleGreen;
dataGridPrevW.Rows[counter].Cells["descrizione"].ToolTipText = "Accettato";
}
if (dataGridPrevW.Rows[counter].Cells["stringaAcconti"].Value != null)
{
dataGridPrevW.Rows[counter].Cells["acconti"].ToolTipText = dataGridPrevW.Rows[counter].Cells["stringaAcconti"].Value.ToString();
}
...
}
and here is the code for CellFormatting:
private void dataGridPrevW_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == dataGridPrevW.Columns["costo"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["acconti"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["complDaIncassare"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["costoLavEseg"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldoFinePr"].Index ||
e.ColumnIndex == dataGridPrevW.Columns["saldolaveseg"].Index
) && e.Value != null)
{
e.CellStyle.Format = "N02";
}
...
}
Thanks again