在Datagridview上加载数据时显示进度条

时间:2010-04-29 09:20:55

标签: c# datagridview

在我的DataGridView上加载数据时,我需要一些显示进度条的帮助。任何示例代码?

感谢。

的问候,

库尔特

1 个答案:

答案 0 :(得分:0)

开始在另一个线程上获取数据,以便不锁定UI线程。您可以在UI上公开一个方法(假设您在此处分层)并从业务层调用该方法。 Windows窗体控件公开一个InvokeRequired标志,您可以使用该标志来检查是否从正确的线程调用控件。如果您没有使用正确的线程,您可以致电代表这样做。

    /// <summary>
    /// Delegate to notify UI thread of worker thread progress.
    /// </summary>
    /// <param name="total">The total to be downloaded.</param>
    /// <param name="downloaded">The amount already downloaded.</param>
    public delegate void UpdateProgressDelegate(int total, int downloaded);

    /// <summary>
    /// Updates the progress in a thread-safe manner.
    /// </summary>
    /// <param name="total">The total.</param>
    /// <param name="downloaded">The downloaded.</param>
    public void UpdateProgress(int total, int downloaded)
    {
        // Check we are on the right thread.
        if (!this.InvokeRequired)
        {
            this.ProgressBar.Maximum = total;
            this.ProgressBar.Value = downloaded;
        }
        else
        {
            if (this != null)
            {
                UpdateProgressDelegate updateProgress = new UpdateProgressDelegate(this.UpdateProgress);

                // Executes a delegate on the thread that owns the control's underlying window handle.
                this.Invoke(updateProgress, new object[] { total, downloaded });
            }
        }
    }

或者您可以使用BackgroundWoker;)