从另一个线程更新时,DataGridView 不会重新绘制自身

发布于 2024-12-08 14:13:33 字数 1415 浏览 0 评论 0原文

我在从另一个线程更新 DataGridView 时遇到问题。让我解释一下。当用户单击表单上的按钮时,我需要用一些行填充网格。这个过程需要一些时间,所以我在一个单独的线程中完成它。在启动线程之前,我将 DataGridView.Enabled 属性设置为 false,以防止用户在添加项目时对其进行编辑,并且在工作线程结束之前我设置了>启用返回到true

问题是如果需要显示滚动条,DataGridView 将无法正确更新其内容。我将用屏幕截图来说明这一点:

partially Drawn row

如您所见,最后一个可见行是部分绘制的,并且DataGridView 不会向下滚动。如果我调整网格大小,使其重新绘制自身,所有行都会正常显示。

这是一些代码:

    private void button1_Click(object sender, EventArgs e)
    {
        string[] fileNames = new string[] { "file1", "file2", "file3" };
        Thread AddFilesToListThread = new Thread(ThreadProcAddRowsToGrid);
        dataGridView1.Enabled = false;
        AddFilesToListThread.Start(fileNames);
    }

    delegate void EmptyDelegate();

    private void ThreadProcAddRowsToGrid(object fileNames)
    {
        string[] files = (string[])fileNames;
        foreach (string file in files)
        {
            EmptyDelegate func = delegate
            {
                dataGridView1.Rows.Add(file);
            };
            this.Invoke(func);
        }

        EmptyDelegate func1 = delegate
        {
            dataGridView1.Enabled = true;
        };
        this.BeginInvoke(func1);
    }

我还注意到只有 Enabled 属性会导致这种奇怪的行为。例如,更改 BackgroundColor 效果很好。

你能帮我看看问题出在哪里吗?

I have a problem updating DataGridView from another thread. Let me explain. When user clicks a button on a form I need to populate the grid with some rows. This process takes some time, so I'm doing it in a separate thread. Before starting the thread I set DataGridView.Enabled property to false, to prevent user from editing items while they are being added, and just before the working thread ends I set Enabled back to true.

The problem is DataGridView won't update its contents correctly if scrollbars need to be shown. I'll illustrate this with a screenshot:

partially drawn row

As you can see, the last visible row is partially drawn and the DataGridView won't scroll down. If I resize the grid, making it to repaint itself, all rows appear normally.

Here is some code:

    private void button1_Click(object sender, EventArgs e)
    {
        string[] fileNames = new string[] { "file1", "file2", "file3" };
        Thread AddFilesToListThread = new Thread(ThreadProcAddRowsToGrid);
        dataGridView1.Enabled = false;
        AddFilesToListThread.Start(fileNames);
    }

    delegate void EmptyDelegate();

    private void ThreadProcAddRowsToGrid(object fileNames)
    {
        string[] files = (string[])fileNames;
        foreach (string file in files)
        {
            EmptyDelegate func = delegate
            {
                dataGridView1.Rows.Add(file);
            };
            this.Invoke(func);
        }

        EmptyDelegate func1 = delegate
        {
            dataGridView1.Enabled = true;
        };
        this.BeginInvoke(func1);
    }

I've also notices that only Enabled property causes this strange behaviour. Changing, for example, BackgroundColor works fine.

Could you help me see where the problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-12-15 14:13:33

您是否尝试过 DataGridView.Refresh()

也许设置只读属性而不是 dataGridView1.Enabled = true;?

或者,我认为这可以通过将数据与用户界面分离来解决。

在我看来,这是一个简化的示例,但如果可以的话,我建议替换等效的行;

dataGridView1.Rows.Add(文件);

然后

DataTable table = getData(); //In your snippet (file)
BindingSource source = new BindingSource();
source.DataSource = table
dataGridView1.Datasource = source;

您还可以使用 BindingSource 上的 ResetBindings 刷新数据;

table = getData();; //Update your data object
source.ResetBindings(false);

Have you tried DataGridView.Refresh()

Maybe setting the readonly property instead of dataGridView1.Enabled = true;?

Alternatively, I think this may be solved by separating your data from the UI.

It looks to me like this is a simplified example for SO here but if you can I would suggest replacing the equivalent line;

dataGridView1.Rows.Add(file);

with

DataTable table = getData(); //In your snippet (file)
BindingSource source = new BindingSource();
source.DataSource = table
dataGridView1.Datasource = source;

Then you can also refresh the data using ResetBindings on the BindingSource;

table = getData();; //Update your data object
source.ResetBindings(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文