RyanJMcGowan
RyanJMcGowan

Reputation: 1515

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.

Upvotes: 2

Views: 694

Answers (4)

user1436963
user1436963

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

IrishChieftain
IrishChieftain

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

SouthShoreAK
SouthShoreAK

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

danielQ
danielQ

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

Related Questions