单元格格式化会减慢填充datagridview的速度? [英] cell formatting slows down populating datagridview?

查看:83
本文介绍了单元格格式化会减慢填充datagridview的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个问题,一个问题,dg是我使用创建的绑定源填充的datagridview

这是代码

Hello, I have a problem and a question, dg is datagridview that I have which I populate with created binding source

this is the code

daActivity.SelectCommand = new SqlCommand("SELECT * from DataActivity", cs);
dsActivity.Clear();
daActivity.Fill(dsActivity);

ActivityBS.DataSource = dsActivity.Tables[0];

dg.DataSource = ActivityBS;


在单元格格式化事件中,我有此代码


and on cell formatting event I have this code

for (int i = 0; i < dg.RowCount; i++)
            {
                string status = dg.Rows[i].Cells[12].Value.ToString();
                if (status == "OPEN")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightCoral;
                }
                if (status == "INFO")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;
                }
                if (status == "CLOSED")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGreen;
                }
            }


并且此事件会减慢填充datagridview(dg)的速度...为什么?有没有一种方法可以解决这个问题,或者我的代码中有什么问题.... well,我需要帮助和意见.


and this event slows down populating datagridview (dg)......why???? is there a way to overcome this, or maybe something in my code or ....well I need assistance and opinion

推荐答案

很难确定没有运行它,但是您应该注意到的第一件事是CellFormating是在每个单元的基础上调用的:由于每次调用它时都遍历所有行,因此您可能会发现这是速度降低的根源.

但是,要做的第一件事就是获取一些数字-直到您对在哪里花费了多少时间有了一个好主意之前,您无法确定自己在进行任何真正的改进.

查看 Stopwatch类 [
It''s difficult to be sure without running it, but the first thing you should notice is that CellFormating is called on a per-cell basis: since you are looping through all the rows every time it is called, you may find this to be a source of slowdown.

The first thing to do though, is to get some numbers - until you have a good idea of how much time is being spent where, you can''t be sure that you are making any real improvement.

Look at the Stopwatch class[^] - it provides you with an accurate method of timing from point to point. Start with a timer around the DataSource setting, and then add one in the CellFormatting event. See what numbers you get, and you can start working out where things are slow.


这篇关于单元格格式化会减慢填充datagridview的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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