0
我遇到了一個奇怪的問題,我第一次嘗試在虛擬DataGridView。網格顯示來自DataTable的數據,行根據狀態列的值進行顏色編碼。我使用事件RowPostPaint根據其狀態值設置每行的顏色。虛擬DataGridView與多種顏色的行導致CellValueNeeded比賽
只要網格中的行在所有列中具有相同的顏色,此功能就很有用。但是,當我在每行中引入多種顏色時,網格變得瘋狂並持續觸發CellValueNeeded事件,並且CPU使用率高達100%。只要多色的行滾動出來,問題就會消失。
DataGridView在常規模式下沒有問題。但是,我真的很想使用虛擬DataGridView來實現最佳性能。有沒有人知道這個問題以及如何避免它?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
namespace VirtualDGV
{
public partial class Form1 : Form
{
protected Color greenColor = Color.FromArgb(200, 240, 140);
protected Color redColor = Color.FromArgb(255, 100, 50);
protected Color cyanColor = Color.FromArgb(175, 255, 225);
protected Color pinkColor = Color.FromArgb(255, 200, 150);
private DataTable data = null;
public Form1()
{
InitializeComponent();
LoadData2();
dgv.VirtualMode = true;
dgv.CellValueNeeded += new DataGridViewCellValueEventHandler(dgv_CellValueNeeded);
dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);
}
private void CreateDataTable()
{
if (data != null)
{
data.Clear();
data.Dispose();
data = null;
}
data = new DataTable();
List<DataColumn> columns = new List<DataColumn>();
columns.Add(new DataColumn("A", typeof(string)));
columns.Add(new DataColumn("B", typeof(string)));
columns.Add(new DataColumn("C", typeof(DateTime)));
columns.Add(new DataColumn("D", typeof(DateTime)));
columns.Add(new DataColumn("E", typeof(string)));
columns.Add(new DataColumn("F", typeof(double)));
columns.Add(new DataColumn("G", typeof(double)));
columns.Add(new DataColumn("H", typeof(string)));
columns.Add(new DataColumn("I", typeof(string)));
columns.Add(new DataColumn("J", typeof(int)));
columns.Add(new DataColumn("Status", typeof(int)));
data.Columns.AddRange(columns.ToArray());
}
private void LoadData1()
{
CreateDataTable();
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 2);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 1);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 5);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 5);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 5);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
dgv.RowCount = data.Rows.Count;
}
private void LoadData2()
{
CreateDataTable();
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 9);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
data.Rows.Add("Abcdefghijklmnopqrstuvwxyz", "XYZ", DateTime.Now, DateTime.Now, "ZZZ", 110, 110, "", "", 250, 0);
dgv.RowCount = data.Rows.Count;
}
protected void SetRowColor(DataGridViewRow row, int status, int dateColumn, int amountColumn)
{
switch (status)
{
case 10:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = greenColor;
row.Cells[dateColumn].Style.BackColor = Color.FromArgb(255, 170, 130);
break;
case 11:
row.Cells[dateColumn].Style.BackColor = Color.FromArgb(255, 170, 130);
row.Cells[amountColumn].Style.BackColor = greenColor;
break;
case 1:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = greenColor;
break;
case 2:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = greenColor;
row.Cells[amountColumn].Style.BackColor = pinkColor;
break;
case 3:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = greenColor;
row.Cells[dateColumn].Style.BackColor = Color.FromArgb(255, 170, 130);
break;
case 4:
row.Cells[dateColumn].Style.BackColor = Color.FromArgb(255, 170, 130);
row.Cells[amountColumn].Style.BackColor = Color.FromArgb(255, 200, 150);
break;
case 5:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = redColor;
break;
case 6:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = cyanColor;
break;
case 7:
row.Cells[amountColumn].Style.BackColor = greenColor;
break;
case 8:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = cyanColor;
break;
case 9:
for (int i = 1; i < row.Cells.Count; i++)
row.Cells[i].Style.BackColor = greenColor;
row.Cells[dateColumn].Style.BackColor = redColor;
row.Cells[amountColumn].Style.BackColor = redColor;
break;
}
}
private void dgv_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
//Debug.WriteLine(e.RowIndex + ", " + e.ColumnIndex);
e.Value = data.Rows[e.RowIndex][e.ColumnIndex];
}
private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
SetRowColor(dgv.Rows[e.RowIndex], (int)data.Rows[e.RowIndex]["Status"], 4, 7);
}
}
}
處理事件CellFormatting工作!非常感謝! – Sebba 2012-07-27 08:26:35