devexpress进度条performtep方法没有显示

时间:2012-11-30 11:02:59

标签: c# .net devexpress

我需要将excel表中的记录上传到sql server 上传时我需要在devexpress进度条中显示进度 而且这种方式也可以像上传100个中的1个一样,并且应该增加直到完成

1 个答案:

答案 0 :(得分:4)

创建BackgroundWorker并将进度报告回主GUI线程。然后使用来自BackgroundWorker的状态更新进度条值。

以下是一个示例,它使用常见的WinForms ProgressBar而不是DevExpress ProgressBar,但原理是一样的。

public partial class Progress : Form
{
    readonly BackgroundWorker _worker = new BackgroundWorker();

    public Progress()
    {
        InitializeComponent();

        _worker.WorkerReportsProgress = true;
        _worker.DoWork += _worker_DoWork;
        _worker.ProgressChanged += WorkerProgressChanged;
        _worker.RunWorkerCompleted += WorkerRunWorkerCompleted;

        _worker.RunWorkerAsync();
    }


    private void _worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var worker = sender as BackgroundWorker;

        // Simulate work (uploading Excel records to SQL Server)
        for (var i = 1; i <= 100; i++)
        {
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                break;
            }

            // Upload some data here, Sleep(100) is just an example
            Thread.Sleep(100);

            // Calculate current progress and report
            worker.ReportProgress(i);
        }
    }

    void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        _progressBar.Value = e.ProgressPercentage;
    }

    void WorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        _progressBar.Value = 0;
    }
}