2012-03-30 1 views
1

私はMcdonaldsのコントロールパネルのように、 のクライアントを選択したいと思っています。 Windowsフォーム でC#を使用しています。私が作ったのは、製品 という名前のクラスを作成してForm1.csに入れた後に、データベースから読み取った 製品を置くリストを作成しました。 私はこれを行い、結果をDataGridに表示しました。 私の問題は、製品の選択を にしてからSendボタンをクリックすると、Datagridが自動的にリフレッシュされないということです。私はプログラムを終了しなければならないときに私は 私は変更を見ることができますそれを再起動します。リストを使用してデータベースから情報を収集する場合、データグリッドを自動的にリフレッシュする方法はありますか?

私の質問は、どのようにDatagrid を更新してプログラムを再起動するのか知っていますか?

おかげで非常に多く、私は以下の私のコードを追加します!

[] My_windows_form 1

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 


namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     MySqlConnection conn = new MySqlConnection();  
     String connectionString = "Server=127.0.0.1; Database=mydatabase; Uid=root; Pwd=;";  
     List<products> listproducts = new List<products>(); 

     public string product; 
     public string quantity; 


     public Form1() 
     { 
      InitializeComponent();    
      startConn(); 
     } 
     private void startConn() 
     { 
      try 
      { 
       conn.ConnectionString = connectionString; 
       conn.Open();     
       textBox3.Text= "Correct connection"; 
       //we call the function READ 
       read(); 
      } 
      catch (MySqlException) 
      { 
       textBox3.Text="An error has ocurred"; 
      } 
     }  
     public void read() 
     { 
      MySqlCommand instruccio = conn.CreateCommand(); 
      instruccio.CommandText = "Select * from products"; 
      MySqlDataReader search = instruccio.ExecuteReader(); 
      while (search.Read()) 
      { 
       products prod = new products(); 
       prod.IdProd = search["idProd"].ToString();      
       prod.Name = search["nomProd"].ToString(); 
       prod.Quantity = Int32.Parse(search["quantitat"].ToString()); 
       listproducts.Add(prod); 
      } 
      dataGridView1.DataSource = listproducts; 

      search.Close(); 
      search.Dispose(); 

     }  
     private void btnEnviar_Click(object sender, EventArgs e) //Button Send (Enviar in spanish) 
     { 
      try 
      {   
       //before updating the stock in database we query the total quantity of the product selected 
       MySqlCommand instruccio1 = connexio.CreateCommand();    
       instruccio1.CommandText = "Select quantitat from productes where `nomProd`='"+ this.product +"'";    
       MySqlDataReader read = instruccio1.ExecuteReader();    
       int result = 0;    
       while (read.Read()) 
       { 
        resultat=Int32.Parse(read["quantitat"].ToString());      
       } 
       read.Dispose(); 
       instruccio1.Dispose(); 

       if (this.quantity != 0) 
       {     
        if (result > this.quantity) 
        { 

        int difference = result - this.quantity; 
        MySqlCommand instruccio2 = conn.CreateCommand(); 
        instruccio2.CommandText = "UPDATE products set `quantitat`='" + this.difference + "' where products.nomProd='" + this.product + "'"; 
        instruccio2.ExecuteNonQuery();         
        conn.Close(); 
            startConn(); 
        textBox1.Text= ""; 
        textBox2.Text = ""; 
        this.quantity = ""; 
        this.product = "";        
       } 
       else 
       { 
        MessageBox.Show("There's no quantity.");     
       }    
      } 
      catch (Exception xe) 
      { 
       MessageBox.Show("",xe.Message); 
      }   
     } 
     private void btnEsborrar_Click(object sender, EventArgs e) //Erase button 
     { 
      this.quantity = ""; 
      this.product = ""; 
      this.aEnviar = 0; 
      textBox1.Text = quantity; 
      textBox2.Text = product; 
     } 

.... 
.... 

答えて

0

をあなたができることは以下の通りです:

  1. タイマーを追加します。あなたのフォームのコントロールとTickイベントを発生させる時間を設定するには、Intervalプロパティのミリ秒単位で入力します。
  2. このイベントでは、startConnという関数を呼び出します。

    dataGridView1.DataSource = listproducts; 
    

し、それをthatsの、あなたのread()方法では非常に簡単:)

+0

あなたの返答をありがとう、私はあなたが言ったことを試みたが動作しませんでした。一方、私は接続を閉じて、startConn関数を呼び出した直後に更新を試みましたが、うまくいきませんでした。私は何が起こっているのか分からない:( – user1298984

+1

この行を追加しようとする: "リスト listproducts = newリスト();"あなたのread()メソッド内 –

+0

ありがとう非常にありがとう! – user1298984

-1

あなたはいつでもその後、あなたのstartConn()メソッドを呼び出す

dataGridView1.DataSource = null; 

を追加します。

+0

これは彼が自動的にグリッドを更新するのにどのように役立つのですか? –

+0

これは最初に、あなたが追加したリストの新しいインスタンスと新しいインスタンスを作成することなく、更新されたリストを手に入れるのに役立ちます。答えを慎重に読んだ場合、彼は実際には達成できないことを慎重に読んでください。そして、私の答えを慎重に読んでみると、私はあなたに彼が見せてくれたイベントかもしれない、いつでもstartConn()私はこのコメントの教師と一緒に卒業しますか? – Dummy01

+0

ああ、彼は彼の更新後に既にこのメソッドを呼び出して以来、どこでもstartConn()を呼び出す必要はありません。明らかに彼は彼のデータグリッドの変更を見ることができませんでした。 – Dummy01

関連する問題