2016-02-03 39 views
0

我使用visual studio 2012:c#winform。我有一個datagridview其中創建了2列(Id和社會)。 datagridview包含了25個記錄提取了我的數據庫。如何從另一個創建一個datagridview?

我還添加了一個名爲「行動」,以每行按鍵以datagridview 注:在我phpmyadmin數據庫中,每個社會包含許多服務類別和服務類型

我的問題是:

我當按下按鈕「動作」時,所有與該社團相關的服務類別和服務類型都會出現在相同或另一個datagridview中。

你能幫我嗎?

回答

0

嘗試類似的東西,你必須啓動這個函數與你的主要DataGridView,你會有你的「克隆」的DataGridView。

public DataGridView CloneDataGrid(DataGridView mainDataGridView) 
{ 
    DataGridView cloneDataGridView = new DataGridView(); 

     if (cloneDataGridView.Columns.Count == 0) 
     { 
      foreach (DataGridViewColumn datagrid in mainDataGridView.Columns) 
      { 
       cloneDataGridView.Columns.Add(datagrid.Clone() as DataGridViewColumn); 
      } 
     } 

     DataGridViewRow dataRow = new DataGridViewRow(); 

     for (int i = 0; i < mainDataGridView.Rows.Count; i++) 
     { 
      dataRow = (DataGridViewRow)mainDataGridView.Rows[i].Clone(); 
      int Index = 0; 
      foreach (DataGridViewCell cell in mainDataGridView.Rows[i].Cells) 
      { 
       dataRow.Cells[Index].Value = cell.Value; 
       Index++; 
      } 
      cloneDataGridView.Rows.Add(dataRow); 
     } 
     cloneDataGridView.AllowUserToAddRows = false; 
     cloneDataGridView.Refresh(); 


    return cloneDataGridView; 
} 

希望這有助於

0

如果我正確理解你的問題,你可以使用一個單元格的內容點擊事件與過濾後的結果來填充你的數據表。

我已經完成了我的一個表格(見下文),但我沒有使用phpmyadmin數據庫,只有SQL,但邏輯可能是相同的。

我也應該注意到,我有默認的單元格選擇作爲全行選擇。

爲了討論起見,我們假設表單顯示產品,比如ProductID,ProductName和ProductType。當我們點擊網格中的按鈕時,我們希望它通過所選行的ProductType進行過濾。

//First we declare a static string that can be accessed by the whole form 
//and blank it so the form doesnt use a previous value. 
public static string product_type = "" 

//In the form load event, the gridview is populated - this could be moved to anywhere. 
private void Form1_Load(object sender, EventArgs e) 
{ 
    SqlConnection conn = new SqlConnection("DB connection string"); 
    conn.Open(); 
    SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn); 
    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    sqlDataAdap.Fill(ds); 
    DataTable dt = new DataTable(); 
    sqlDataAdap.Fill(dt); 
    conn.Close(); 
} 

//In the cell contect click event we give it the ProductType 
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    //Here we declare a var to identify that we're looking at the row 
    var row = dataGridView1.CurrentRow.Cells; 
    //Now we set the string we declared earlier 
    product_type = Convert.ToString(row["ProductType"].Value); 

    //Now we repeat the grid script with a parameter which uses our string from above 
    SqlConnection conn = new SqlConnection("DB connection string"); 
    conn.Open(); 
    SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE ProductType = @ProductType", conn); 
    sc.Parameters.Add("@ProductType", SqlDbType.Int).Value = product_type; 
    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    sqlDataAdap.Fill(ds); 
    DataTable dt = new DataTable(); 
    sqlDataAdap.Fill(dt); 
    conn.Close(); 
} 

然後,您可以只添加一個按鈕,點擊返回電網,以它的非過濾狀態時,從窗體加載事件重複腳本。

希望這會有所幫助。

相關問題