2013-02-08 56 views
-2

將數據從一個表格傳輸到另一個表格時,我想要一個進度條在我的表格上顯示狀態。我嘗試了一些方法,但它不起作用。如何在我的winform的進度條上顯示狀態?

if (dataGridView1.RowCount - 1 == no_Of_rows) 
{ 
    progressBar1.Value = 100; 
    dataGridView1.Visible = true; 
} 
else 
{ 
    progressBar1.Value = 50; 
    dataGridView1.Visible = false; 
} 
+2

顯示代碼並告訴是**什麼**不工作。 –

+1

使用BackgroundWorker,查找類,從那裏開始。 – Derek

+0

如果(dataGridView1.RowCount - 1個== no_Of_rows) { progressBar1.Value = 100; dataGridView1.Visible = true; } 別的 { progressBar1.Value = 50; dataGridView1.Visible = false; } – janu

回答

0

下面是一個例子,如何使用的BackgroundWorker。

private BackgroundWorker worker; 
    public Form1() 
    { 
     InitializeComponent(); 


     this.worker = new BackgroundWorker 
      { 
       WorkerReportsProgress = true 
      }; 
     worker.DoWork += WorkerOnDoWork; 
     worker.ProgressChanged += WorkerOnProgressChanged; 
     worker.RunWorkerCompleted += delegate 
      { 
       //Set the value of the progressbar to the maximum value if the work is done 
       this.progressBar1.Value = this.progressBar1.Maximum; 
      }; 
     worker.RunWorkerAsync(); 
    } 

    private void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     //Set the value of the progressbar, or increment it. 
     //You can use the e.ProgressPercentage to get the value you set in the DoWork-Method 
     //The e.UserState ist a custom-value you can pass from the DoWork-Method to this Method 
     this.progressBar1.Increment(1); 
    } 


    private void WorkerOnDoWork(object sender, DoWorkEventArgs e) 
    { 
     // Do you stuff here. Here you can tell the backgroundworker to report the progress like. 
     this.worker.ReportProgress(5); 
     //You can not access properties from here, so if you want to pass a value or something else to the 
     //progresschanged-method you have to use e.Argument. 

    } 

這是一個winforms應用程序。在窗體上只有一個名爲progressbar1的進度條