简体   繁体   English

ASP.NET关系数据库

[英]ASP.NET Relational database

I have two tables: 我有两个表:

Products 制品
-ProductID, -ProductID,
-ProductName, -产品名称,
-ProductCategoryID, -ProductCategoryID,

ProductCategories 产品类别
-ProductCategoryID, -ProductCategoryID,
-ProductCategoryName, -ProductCategoryName,

I am using a dataset and I can successfully set up a relationship from the Products [ProductCategoryID] to the ProductCategoryID table. 我正在使用数据集,并且可以成功建立从Products [ProductCategoryID]到ProductCategoryID表的关系。 How do I get the Gridview to show up with ProductCategoryName rather than the integer reference? 如何使Gridview显示为ProductCategoryName而不是整数引用?

I'm accustomed to Access where this just happens by default but it doesn't seem to work that way in Visual Studio. 我习惯于Access在默认情况下会发生,但在Visual Studio中似乎无法正常工作。

Create a table join between your two tables in your query. 在查询中的两个表之间创建一个表联接。 For example, to display a single column: 例如,要显示单个列:

SELECT ProductCategoryName FROM Products
JOIN ProductCategories ON
Products.ProductCategoryID = ProductCategories.ProductCategoryID

myGrid.DataSource = myDataSet;
myDataSet.Bind();

Ok, you need to provide more information on how are you working with the gridview. 好的,您需要提供有关如何使用gridview的更多信息。 Assuming that you are filling everything correctly and that you're working with columns over the .aspx, have you tried 假设您正确填充了所有内容,并且正在使用.aspx上的列,是否尝试过

<asp:GridView ID="yourId" runat="server" AutoGenerateColumns="false"> 
<Columns> 
...
    <asp:BoundField DataField="ProductCategories.ProductCategoryName" HeaderText="Category" /> 
...
</Columns> 

Depending on your implementation, it may be worth considering a viewmodel-based approach. 根据您的实现,可能值得考虑基于视图模型的方法。 Create a new class 创建一个新的班级

ProductModel
-ProductId
-ProductCategoryName
-ProductName

Bind your grid to a collection of this object instead of your database object. 将网格绑定到该对象的集合,而不是数据库对象。 This won't work well for every implementation but it is worth suggesting. 这并非对每个实现都很好,但是值得一提。

select a.ProductID,a.ProductName,b.ProductCategoryName from Products a,ProductCategories b where a.ProductCategoryID=b.ProductCategoryID 从产品a,ProductCategories b中选择a.ProductID,a.ProductName,b.ProductCategoryName,其中a.ProductCategoryID = b.ProductCategoryID

myGrid.DataSource = myDataSet; myGrid.DataSource = myDataSet; myDataSet.Bind(); myDataSet.Bind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM