简体   繁体   中英

ASP.NET Relational database

I have two tables:

Products
-ProductID,
-ProductName,
-ProductCategoryID,

ProductCategories
-ProductCategoryID,
-ProductCategoryName,

I am using a dataset and I can successfully set up a relationship from the Products [ProductCategoryID] to the ProductCategoryID table. How do I get the Gridview to show up with ProductCategoryName rather than the integer reference?

I'm accustomed to Access where this just happens by default but it doesn't seem to work that way in 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. Assuming that you are filling everything correctly and that you're working with columns over the .aspx, have you tried

<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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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