如果我正確理解你的問題,你可以使用一個單元格的內容點擊事件與過濾後的結果來填充你的數據表。
我已經完成了我的一個表格(見下文),但我沒有使用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();
}
然後,您可以只添加一個按鈕,點擊返回電網,以它的非過濾狀態時,從窗體加載事件重複腳本。
希望這會有所幫助。