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

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

问题描述

我有一个 DataGridView的中,我从一个SQL Server数据库加载数据。当我加载数据需要花费相当长的时间。

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的进度

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.

我看到有人授予我的问题与赏金。我想说的是,目前荫使用此代码,如果它会适合我appriciate。

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



把方法将数据装入另一个线程和设定当它完成了数据源。应该有一个需要调用。如果你想显示百分比值的进度,不要使用风格'帐篷'和添加其他功能,并委托可以调用设置进度条的值。

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.

我希望这有助于。

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

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