DataGridView问题(可能是双重持续?)
我有一个问题: 在Windows表单C#App中,我有一个带有CellFormatting事件的DataGridView的正常表单(我需要格式化5-6列;有1个一切都可以)。
问题是表单未完全加载/渲染(某些元素没有正确的颜色,标签不完整)。 DataGridView(带2行)也存在一些问题(当我单击带有背色格式的单元格时,此颜色会在另一行上)。
我已经启用了双重手段(使用新类dbdatagridview的解决方案可在此处提供: 如何防止datagridview在smovelling scrolling scrolling scrolling scrolling flokolling flokolling flokolling flokolling时) 但这仍然行不通。
我已经注意到,在DataGridView的属性中,有这2: - doublebuffered:是的 -doublebufferedEnabled:false
可能是这个问题吗?如何设置为Double BufferedEnabled?
我正在使用Visual Studio 2013,版本12.0.3更新4
这些是DataGridView属性:
https://i.sstatic.net/mqbwo.jpg“ alt =“这些是datagridview属性”>
感谢
此处的更新,因为在注释中,不可能附加图像:
是的,我的应用程序是winforms。 这是有效的代码(“单元格”在外部定义为“ private datagridviewcell cell = null;”)。 有些列是隐藏的(例如“ Accettato”),我用它们将其他列格式化。
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;
}
} */
}
这就是结果(可以使用注释的代码):
,这是没有注释的结果(ko),我的意思是整个未注册的代码:
可以看出,在顶部元素没有完全加载(有绿色零件,“加”按钮出现2次,标题是不同的 - “ elenco cartelle per paziente”而不是“ cartella paziente”,这是以前的形式)。 这三行是可以的,并由未注册的线进行格式化(我认为这里也有问题,因为如果我单击绿色行,绿色会消失,如果我单击另一排,它将再次出现)。
更新:看来问题本身是因为顶部元素不在DataGridView中...我还尝试在表单上设置DoubleBuffered = true,但我得到了相同的行为。 也许是因为datagridview毫无疑问被缓冲,那么表单未完全加载?
更新:当单击该行是“正常”时,绿色消失的问题……这意味着设置交替的RowColor覆盖了单元格(在细胞形成之前或其他地方设置)的颜色。 我已经以这种方式解决了:
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;
}
}
}
这是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();
}
...
}
这是用于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";
}
...
}
再次感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论