Oxy*_*ron 5 c# datagrid multithreading flicker winforms
在我正在开发的应用程序中,我正在使用datagridview来显示数据.为了填充它,我要按一个按钮,后台工作人员将开始运行,它将填充一个数据表,当它完成运行时,它将使用数据表作为数据网格的数据源.这很好用,用户界面保持响应等等.但是现在我已经对行进行了着色,这取决于它们的值(我还在玩它,所以欢迎任何建议):
private void ApplyColoring()
{
if (dataGridView1.DataSource != null)
{
foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
{
// hardmap a color to a column
IDictionary<Int32, Color> colorDictionary = new Dictionary<Int32, Color>();
colorDictionary.Add( 7, Color.FromArgb(194, 235, 211));
colorDictionary.Add( 8, Color.Salmon);
colorDictionary.Add( 9, Color.LightBlue);
colorDictionary.Add(10, Color.LightYellow);
colorDictionary.Add(11, Color.LightGreen);
colorDictionary.Add(12, Color.LightCoral);
colorDictionary.Add(13, Color.Blue);
colorDictionary.Add(14, Color.Yellow);
colorDictionary.Add(15, Color.Green);
colorDictionary.Add(16, Color.White);
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in gridRow.Cells)
{
if (colorDictionary.Keys.Contains(cell.ColumnIndex))
{
// standard background
cell.Style.BackColor = Color.FromArgb(194, 235, 211);
}
}
}
IList<String> checkedValues = new List<String>();
// first we loop through all the rows
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
IDictionary<String, Int32> checkedVal = new Dictionary<String, Int32>();
// then we loop through all the data columns
int maxCol = dnsList.Count + 7;
for (int columnLoop = 7; columnLoop < maxCol; columnLoop++)
{
string current = gridRow.Cells[columnLoop].Value.ToString();
for (int checkLoop = 7; checkLoop < maxCol; checkLoop++)
{
string check = gridRow.Cells[checkLoop].Value.ToString();
if (!current.Equals(check))
{
if (checkedVal.Keys.Contains(current))
{
gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[checkedVal[current]];
}
else
{
gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[columnLoop];
checkedVal.Add(current, columnLoop);
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一些问题.不是因为着色不起作用,确实如此.但是因为它让它变得很慢.它第一次运行正常,但当我再次按下按钮时,它很慢,因为地狱和数据网格闪烁.我希望这个运行作为后期处理,所以它(或者应该)在后台工作完成后运行.但是当我从RunWorkerCompleted事件中调用applycoloring时,它只是很慢.我应该怎么做才能防止这种情况发生?如何在执行新查询时确保UI不闪烁(同时不丢失网格中的当前数据).
小智 18
你可以打开双缓冲.
VB:
Imports System.Reflection
Run Code Online (Sandbox Code Playgroud)
将以下内容放在Form_Load中
Dim systemType As Type = DataGridView1.GetType()
Dim propertyInfo As PropertyInfo = systemType.GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic)
propertyInfo.SetValue(DataGridView1, True, Nothing)
Run Code Online (Sandbox Code Playgroud)
对于C#:转到:修复慢速滚动DataGridView
干杯
两个建议:
我找到了另一种对慢速数据网格进行反射双缓冲的方法:
创建一个带有一些反射的扩展方法来在数据网格上设置双缓冲:
public static class DataGridViewExtensioncs
{
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
var dgvType = dgv.GetType();
var pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在表单初始化程序中执行以下操作:
this.dataGrid.DoubleBuffered(true);
Run Code Online (Sandbox Code Playgroud)
这与 Evolved 的回答非常相似,学分转到 Shweta Lodha:http : //www.codeproject.com/Tips/390496/Reducing-flicker-blinking-in-DataGridView