使用 DataTable 将数据加载到 DataGridView 的进度条 [英] Progressbar for loading data to DataGridView using DataTable

查看:21
本文介绍了使用 DataTable 将数据加载到 DataGridView 的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView,我在其中从 SQL 服务器数据库加载数据.当我加载数据时,它需要很长时间.

I have a DataGridView in which I load data from a SQL server database. When I load the data it takes quite long time.

我想向用户提供数据正在加载的信息.请问您在数据加载到DataGridView时连接Progressbar的最佳方式是什么?

I would like to give user information that the data is loading. May I ask you what is the best way connecting Progressbar when data is loading into the DataGridView?

我不希望任何人为我编写完整的代码.我只是想知道怎么做.

I don't want anyone to make a fully working code for me. I just would like to know how it can be done.

我看到有人悬赏我的问题.我想说,目前我正在使用这个代码,如果它适合我​​会很高兴.

I see someone awarded my question with bounty. I would like to say that at the moment Iam using this code which I would appriciate if it would fit.

DTGdataTable = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter
SDA.Fill(DTGdataTable);
dataGridView1.DataSource = DTGdataTable ;

感谢大家的时间.

推荐答案

如果问题是从数据库中获取数据需要很长时间,我有一个可能的解决方案:

If the problem is that it takes long to fetch the data from the database, i have a possible solution for you:

    private void buttonLoad_Click(object sender, EventArgs e)
    {
        progressBar.Visible = true;
        progressBar.Style = ProgressBarStyle.Marquee;
        System.Threading.Thread thread = 
          new System.Threading.Thread(new System.Threading.ThreadStart(loadTable));
        thread.Start();
    }

    private void loadTable()
    {
        // Load your Table...
        DataTable table = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter();
        SDA.Fill(table);
        setDataSource(table);
    }

    internal delegate void SetDataSourceDelegate(DataTable table);
    private void setDataSource(DataTable table)
    {
        // Invoke method if required:
        if (this.InvokeRequired)
        {
            this.Invoke(new SetDataSourceDelegate(setDataSource), table);
        }
        else
        {
            dataGridView.DataSource = table;
            progressBar.Visible = false;
        }
    }

将加载数据的方法放到另一个线程中,完成后设置数据源.应该需要调用.如果您想在进度条中显示百分比值,请不要使用样式Marquee"并添加另一个函数和委托,您可以调用它来设置进度条的值.

Put the method loading the data into another thread and set the datasource when it's finished. There should be an invoke required. If you want to show percentage values in the progressbar, don't use the style 'Marquee' and add another function and delegate you can invoke for setting the value of the progress bar.

如果绑定数据到网格有问题,那么你不能把绑定放到另一个线程中,你可能会显示一个在另一个线程中运行的进度弹出窗口.

If binding the data to the grid is the problem, then you can not put the binding into another thread and you may show a progress-popup that runs in another thread.

我希望这会有所帮助.

这篇关于使用 DataTable 将数据加载到 DataGridView 的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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