0

この質問に以前に尋ねられたかどうかは分かりません。 私はC#のウィンドウフォームでコード化されたフロントエンドアプリケーションを持っています。二番目の形式では私は私のボタンのクリックでもこのようになります をクリックし、単一のボタンで同時に両方のデータグリッドをリフレッシュする必要がある2つの異なるSQL Serverの事前定義ビュー ボタンを1回クリックして複数のデータグリッドビューを更新する

から移入ます2 datagridviewsを持っている..

私はこれがあることを理解どのように
private void RefreshBTN_Click(object sender, EventArgs e) 
    { 
     SqlConnection myConnection = new SqlConnection("removed for illustration only"); 
     string query = "select * from daily_orders order by orderno DESC"; 
     SqlCommand cmd = new SqlCommand(query, myConnection); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     dataGridView1.DataSource = dt; 
    } 

、C#は、新しい接続を開き、必要なデータをdatagridview1充填することにより、DBとリターンを照会します。私は同じクリックイベントが別のSQLビューからデータを要求し、同時に別のdatagridviewを設定したいと思います。視覚的には、両方のグリッドが、同じフォーム上に縦に並べられます。事前

+0

);'や 'datagridview1.refresh();' –

答えて

1

多くのおかげで、別の関数にGRID1をリフレッシュするためのコードを移動し

。次に、貼り付けをコピーし、その機能をGrid2用に複製します。 Grid2の名前と同様にGrid2のSQLを変更します。コピーした関数の名前を2に変更します。次に、両方の関数への呼び出しを追加すると、ボタンをクリックすると両方のグリッドが更新されます。

あなたは( `datagridview1.updateを使用しようとしないのはなぜ
private void RefreshBTN_Click(object sender, EventArgs e) 
{ 
    //call both functions to refresh both on button click 
    RefreshGrid1(); 
    RefreshGrid2(); 
} 

private void RefreshGrid1() 
{ 
    SqlConnection myConnection = new SqlConnection("removed for illustration only"); 
    string query = "select * from daily_orders order by orderno DESC"; 
    SqlCommand cmd = new SqlCommand(query, myConnection); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    dataGridView1.DataSource = dt; 
} 

//give this function a unique name to represent the second grid refresh 
private void RefreshGrid2() 
{ 
    SqlConnection myConnection = new SqlConnection("removed for illustration only"); 
    string query = "select * from daily_orders order by orderno DESC"; 
    SqlCommand cmd = new SqlCommand(query, myConnection); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    //rename this to your second grid name 
    dataGridView2.DataSource = dt; 
} 
+0

うわー!これは完璧です。私は同様の考え方で作業していましたが、グリッドリフレッシュイベントを2つのユニークな機能に分けることは考えていませんでした。どうもありがとう! – Christopher

+0

あなたの非常に歓迎:) – EJD

関連する問題