非常感谢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