非常感謝Henk,您的文章讓我走上了正確的軌道,它完美地解決了我的問題。 我現在可以在任何網格視圖中添加項目,並且我的DataTable已更新,無需執行像我期望的那樣可以完成的任何操作。
爲了嘗試理解解決方案,我做了一個小測試演示,我想我會在這裏發佈給未來的讀者,因爲它包括如何過濾每個DataView以僅包含相關信息。 這是一個示例代碼,我沒有包括錯誤檢查等。
private DataTable fruitsDataTable = null;
private DataView orangesDataView = null;
private DataView applesDataView = null;
private void Form1_Load(object sender, EventArgs e)
{
fruitsDataTable = new DataTable("Fruits");
// Dynamically create the DataTable schema for the sake of this example
fruitsDataTable.Columns.Add("Category", typeof(string));
fruitsDataTable.Columns.Add("Description", typeof (string));
fruitsDataTable.Columns.Add("Quantity", typeof(int));
fruitsDataTable.Columns.Add("Price", typeof(double));
// Add the fruits to the main table
fruitsDataTable.Rows.Add("ORANGE", "Fresh Oranges", 5, 5.50);
fruitsDataTable.Rows.Add("APPLE", "Granny Smith Apples", 10, 2.20);
fruitsDataTable.Rows.Add("APPLE", "Golden Apples", 40, 1.75);
fruitsDataTable.Rows.Add("ORANGE", "Bloody Oranges", 10, 7.99);
fruitsDataTable.Rows.Add("BANANA", "Ivory Coast Bananas", 5, 6.99);
mainGridView.DataSource = fruitsDataTable;
// Create a DataView for each fruit category and bind it to the relevant DataGridView control on the form
orangesDataView = new DataView(fruitsDataTable, "Category = 'ORANGE'", string.Empty, DataViewRowState.CurrentRows);
orangesGridView.DataSource = orangesDataView;
applesDataView = new DataView(fruitsDataTable, "Category = 'APPLE'", string.Empty, DataViewRowState.CurrentRows);
applesGridView.DataSource = applesDataView;
}
謝謝,請你再擴展一下嗎? – 2010-08-09 09:09:52