我有3DataSet
张桌子Categories
和. 我可以单独访问它们。但我想将它们绑定在 3 个不同的地方,这样,更改类别反映项目,更改项目反映产品。我想这可以通过创建表之间的关系来完成。请帮助我。Items
Products
ComboBoxes
问问题
98 次
2 回答
0
您可以通过 Dataset.Relation 参考此内容并尝试在您的代码中实现这一点
// Define the relationship between Categories and Products.
DataRelation relat = new DataRelation("CatProds",
ds.Tables["Categories"].Columns["CategoryID"],
ds.Tables["Products"].Columns["CategoryID"]);
// Add the relationship to the DataSet.
ds.Relations.Add(relat);
于 2013-05-10T05:55:09.310 回答
0
有很多方法可以用 ADO.NET 数据集给这只猫换皮,但是您可以使用DataSet.Relations
. 查看MSDN 文档以获取更多信息。
这是您概述的关系的示例,并且仅将其沿一条可能的路径走得足够远才能给您这个想法:
var ds = new DataSet();
// Add the Categories table with a CategoryID column, and add a few rows to it.
var categories = ds.Tables.Add("Categories");
var categoryIDColumn = categories.Columns.Add("CategoryID", typeof(int));
categories.Rows.Add(new object[] { 1 });
categories.Rows.Add(new object[] { 2 });
categories.Rows.Add(new object[] { 3 });
// Add the Items table with ItemID & CategoryID (FK) columns, and add a few Categories-relatable rows to it.
var items = ds.Tables.Add("Items");
var itemIDColumn = items.Columns.Add("ItemID", typeof(int));
var itemCategoryIDColumn = items.Columns.Add("CategoryID", typeof(int));
items.Rows.Add(new object[] { 11, 1 });
items.Rows.Add(new object[] { 12, 2 });
items.Rows.Add(new object[] { 13, 3 });
// Add the Products table with ProductID & ItemID (FK) columns, and add a few Items-relatable rows to it.
var products = ds.Tables.Add("Products");
var productIDColumn = products.Columns.Add("ProductID", typeof(int));
var productItemIDColumn = products.Columns.Add("ItemID", typeof(int));
products.Rows.Add(new object[] { 21, 11 });
products.Rows.Add(new object[] { 22, 12 });
products.Rows.Add(new object[] { 23, 13 });
// Add the FK relationships (or data relations as it were).
var categoryItemsRelation = ds.Relations.Add("FK_CategoryItems", categoryIDColumn, itemCategoryIDColumn);
var itemProductsRelation = ds.Relations.Add("FK_ItemProducts", itemIDColumn, productItemIDColumn);
// Run through an example of a selected ID in the categories dropdown.
int selectedCategoryID = 1;
var selectedCategory = categories.Select(String.Format("CategoryID = {0}", selectedCategoryID))[0];
var filteredItems = selectedCategory.GetChildRows(categoryItemsRelation);
Console.WriteLine("selectedCategoryID == {0}", selectedCategoryID);
Console.WriteLine("selectedCategory[categoryIDColumn] == {0}", selectedCategory[categoryIDColumn]);
foreach (var childItem in filteredItems)
{
Console.WriteLine("filteredItems includes:");
Console.WriteLine(
"\t{0} : {1}, {2} : {3}",
itemIDColumn.ColumnName,
childItem[itemIDColumn],
itemCategoryIDColumn.ColumnName,
childItem[itemCategoryIDColumn]);
}
// ...and so forth.
于 2013-05-10T06:26:41.150 回答