[英]Refreshing multiple datagridviews with single button click
不知道是否曾經問過這個問題,所以去吧。.我有一個前端應用程序,以C#Windows窗體編碼。 在第二種形式上,我有兩個從兩個不同的sql server預定義視圖填充的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來返回。 我希望同一個click事件從另一個sql視圖請求數據並同時填充另一個datagridview。 視覺上,兩個網格都在同一形狀上垂直對齊,一個與另一個對齊。
提前謝謝了
將用於刷新Grid1的代碼移到一個單獨的函數中。 然后復制粘貼並為Grid2復制該功能。 更改Grid2的SQL以及Grid2的名稱。 用2重命名復制的函數。然后添加對這兩個函數的調用,因此單擊按鈕將刷新兩個網格。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.