Reputation: 1515
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.
Upvotes: 2
Views: 694
Reputation: 1
select a.ProductID,a.ProductName,b.ProductCategoryName from Products a,ProductCategories b where a.ProductCategoryID=b.ProductCategoryID
myGrid.DataSource = myDataSet; myDataSet.Bind();
Upvotes: 0
Reputation: 15253
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();
Upvotes: 1
Reputation: 4296
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.
Upvotes: 0
Reputation: 2086
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>
Upvotes: 0