2011-12-09 76 views
10

我想在從SQL數據庫讀取數據時通知用戶 並且我決定使用進度條創建表單但它不起作用 - 可能是因爲需要線程。我想以編程方式創建表單在執行SQL查詢時顯示進度條

 ProgressBar pb = new ProgressBar(); 

     pb.MarqueeAnimationSpeed = 30; 
     pb.Style = ProgressBarStyle.Marquee; 
     pb.Dock = DockStyle.Fill; 

     progressForm.ClientSize = new Size(200, 50); 
     progressForm.FormBorderStyle = FormBorderStyle.FixedDialog; 
     progressForm.StartPosition = FormStartPosition.CenterScreen; 
     progressForm.Controls.Add(pb); 
     progressForm.ControlBox = false; 
     progressForm.TopMost = true; 

     progressForm.Show(); 
     //do data processes here (all queries and executes) 
     progressForm.close(); 

如何修改上述代碼以實現我所述的目標?

編輯:順便說一句,我想在我的項目中的每個數據函數中使用此進度條窗體。例如:fillGrid,runQuery ..

@非常感謝你的回答。我的意思是我該如何使用類的功能,例如我的gridFill功能是在連接類:從另一種形式

class ConnectionClass 
    { 
     public static SqlConnection connection = new SqlConnection(); 

    public string sorgu; 
    public static string server; 
    public static string userId; 
    public static string catalog; 
    public static string password; 
    public static string accessMethod; 
    public DataSet ds = new DataSet(); 
    Form progressForm = new Form();  

    public bool Open() 
    { 
     try 
     { 
      if (connection.State != ConnectionState.Open) 
      { 

       connection.ConnectionString = "Data Source = " + server + ";" + 
               "Initial Catalog=" + catalog + ";" + 
               "User ID=" + userId + ";" + 
               "Password=" + password + ";" + 
               "Connect Timeout=0"; 

       connection.Open(); 
       return true; 
      } 
      else 
      { 
       return true; 
      } 


     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("System message:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return false; 
     } 

    } 

    public DataTable Dt(string query) 
    { 
     DataTable dt = new DataTable(); 
     if (Open()) 
     { 
      SqlDataAdapter da = new SqlDataAdapter(query, connection); 
      try 
      { 
       //progressForm.Showdialog() is this possible??? 
       da.Fill(dt); 
       //progressForm.close(); ?? 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Sistem Mesajı:" + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     }   
     return dt; 
    } 

    public bool Run(string query, string hataMsj) 
    { 
     Form activeForm = Form.ActiveForm; 
     query = " SET DATEFORMAT DMY " + query; 

     SqlCommand sc = new SqlCommand(query, connection); 
     try 
     { 
      Open(); 
      sc.ExecuteNonQuery(); 
      return true; 
     }   
     catch (Exception) 
     { 
      return false; 
     } 
    } 

    public void fillComboBox(string sorgu, ComboBox cb, string text, string value) 
    { 
     DataTable dt = Dt(sorgu); 

     cb.DisplayMember = text; 
     cb.ValueMember = value; 
     cb.DataSource = dt; 
     if (cb.Items.Count > 0) 
     { 
      cb.SelectedIndex = 0; 
     } 

    } 

    public int fillGridView(string sorgu, DataGridView dgv) 
    { 
     DataTable dtGrvw = Dt(sorgu); 
     dgv.DataSource = dtGrvw; 
     return 1; 
    }  
    } 

和示例查詢(類)

ConnectionClass cc = new ConnectionClass(); 

    query= " INSERT INTO tblPersonel (" + 
              " [sqlUserName] " + 
              ",[personelNo] " + 
              ",[ad] " + 
              ",[soyad] " + 
              ",[departmanId] " + 
              ",[emailadres] " + 
              ",[tcKimlikNo],[kangurubu],[dokumaciNo])VALUES" + 
              "('" + tbSqlUserName.Text + 
              "','" + tbPersonelNo.Text + 
              "','" + tbAd.Text + 
              "','" + tbSoyad.Text + 
              "','" + cbDepartman.SelectedValue.ToString() + 
              "','" + tbMail.Text + 
              "','" + tbKimlikno.Text + 
              "','" + tbKangrubu.Text + 
              "','" + tbDokumaciNo.Text + "') "; 
        if (cc.Run(query, "Unexpected error on insert new person")) 
        { 
         fillGrid(); 
         this.Close(); 

        } 

    public void fillGrid() 
    { 
     query= " select * from View_Personel order by personelNo desc"; 
     cc.fillGridView(query, gridviewPersonel); 
    } 

,我無法想象如何能我在bw_DoWork事件中使用它。因爲我的函數有參數(查詢,gridview),當我從另一個類調用它時,我可以使用它的參數...

p.s. :this Method對我來說很不錯,但它沒有奏效。我不明白這個問題

+7

看看後臺工人類。 http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx –

回答

3

使用BackgroundWorker類來填充你的DataGrid。

 Form progressForm; 

    public void func() { 
     BackgroundWorker bw = new BackgroundWorker(); 
     bw.DoWork += new DoWorkEventHandler (bw_DoWork); 
     bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted); 

     progressForm = new Form(); 

     ProgressBar pb = new ProgressBar(); 

     pb.MarqueeAnimationSpeed = 30; 
     pb.Style = ProgressBarStyle.Marquee; 
     pb.Dock = DockStyle.Fill; 

     progressForm.ClientSize = new Size (200, 50); 
     progressForm.FormBorderStyle = FormBorderStyle.FixedDialog; 
     progressForm.StartPosition = FormStartPosition.CenterScreen; 
     progressForm.Controls.Add (pb); 
     progressForm.ControlBox = false; 
     progressForm.TopMost = true; 

     progressForm.Show(); 

     string queryString = "SELECT ...."; // fill query string here 
     var params = new KeyValuePair<GridControl, string>(sorgu, queryString); 
     bw.RunWorkerAsync (params); 
    } 

    void bw_DoWork (object sender, DoWorkEventArgs e) { 
     KeyValuePair<GridControl, string> params = e.Argument as KeyValuePair<GridControl, string>; 
     ConnectionClass cc = new Connection Class(); 
     cc.fillGrid(params.Value, params.Key); 
    } 

    void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) { 
     progressForm.Close(); // 
    } 

是可能的參數發送到BackgroundWorker的。如果您需要多個參數,您可以發送一個包含您需要的任何對象的元組。

編輯:如果你在3.5,您可以改爲使用KeyValuePair。代碼爲此更新。

+0

感謝您的評論它工作良好。但我有一個小問題。我的數據填充函數是在一個類中,並且需要2個參數(查詢和網格控件),並且我在項目中的任意位置都使用它。 public void fillGridControl(string sorgu,GridControl gc) BindingSource dataSource = new BindingSource(Dt(sorgu) , 空值); gc.DataSource = dataSource; } 我可以把這個函數放在bw_DoWork事件中嗎? – Rapunzo

+1

@Rapunzo你可以簡單地在bw_DoWork事件中調用你的fillGridControl函數。我不知道你的代碼,但是有沒有理由說這不起作用? – Will

+0

我已經更新了我的答案。 – Will

0

正如Ash Burlaczenko建議,你必須使用BackgroundWorker爲此目的。

但是,由於您希望將其與ProgressBar綁定,因此建議您在CodeProject上查看此文章:ProgressWorker

它使用起來相當簡單,它會自動爲您更新進度條。您只需要不時撥打ProgressWorker.ReportProgress方法即可更新關聯的進度條。