开发者

Show Progress bar upon loading data on Datagridview

I need some help on showing Progress bar upon loading data on m开发者_开发知识库y DataGridView. Any sample code?

Thanks.

regards,

kurt


Start the fetching of the data on another thread in order to not lock up the UI thread. You could expose a method on the UI (presume you have separation of layers here) and call that method from your business layer. Windows Form controls expose a InvokeRequired flag that you can use to check if you are calling the control from the correct thread. If you are not on the right thread you can call a delegate to do so.

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

Or you could just use a BackgroundWoker ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜