如何防止数据网格中的行在应用程序运行时闪烁 [英] How to prevent rows in datagrid from flickering while application is running

查看:23
本文介绍了如何防止数据网格中的行在应用程序运行时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的应用程序中,我正在使用 datagridview 来显示数据.要填充它,我必须按下一个按钮,一个后台工作者将开始运行,它将填充一个数据表,当它完成运行时,它将使用该数据表作为数据网格的数据源.这工作正常,用户界面保持响应等.但是现在我已经根据它们的值对行进行了着色(我仍在玩弄它,所以欢迎提出任何建议):

In the application I'm developing at the moment I'm using a datagridview to display data. To fill it, I've to press a button and a backgroundworker will start running, it will fill a datatable and when it's finished running it will use the datatable as the datasource for the datagrid. This works fine, the UI stays responsive et cetera. But now I've implemented coloring to the rows, depending on their values (Im still playing around with it, so any suggestions are welcome):

        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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

这给我带来了问题.不是因为着色不起作用,它起作用了.但因为它让它变慢了.第一次运行正常,但是当我再次按下按钮时,它慢得要命,数据网格也在闪烁.我希望这个作为后处理运行,所以它(或者应该)在后台工作完成后运行.但是当我从 RunWorkerCompleted 事件调用 applycoloring 时,它只是很慢.我应该怎么做才能防止这种情况?如何确保 UI 在执行新查询时不闪烁(同时不会丢失网格中的当前数据).

This is giving me problems. Not because the coloring doesn't work, it does. But because it makes it hella slow. The first time it runs fine, but when i press the button again, it's slow as hell and the datagrid is flickering. I want this run as postprocess, so it's (or rather should) be run after the backgroundworker is completed. But when i call applycoloring from the RunWorkerCompleted event its just slow. What should I do to prvent this? How can I make sure the UI doesnt flicker while its executing a new query (while NOT LOSING the current data in the grid).

推荐答案

两点建议:

  1. 尝试在循环之前调用 SuspendLayout() 并在循环之后调用 ResumeLayout().大多数其他控件将其称为 BeginUpdate() 和 EndUpdate()(列表视图、组合框等).
  2. 如果您要处理大量数据,请在 DataGridView 上使用 VirtualMode数据.

这篇关于如何防止数据网格中的行在应用程序运行时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆