开发者

Displaying a single value from a one to many relationship in a GridView

I have 3 tables:

Order, OrderStates, OrderStateDefinition

An Order has many OrderStates which then has one OrderStateDefinition.

I have a gridview in which I am trying to display only one value inside the OrderStates collection - the latest OrderState that has been added.

I've read a little about subqueries but I'm unsure about how to go about achieving the result I want.

Sorry bout the lack of information, I had a nice picture all set up of the table structure but stackoverflow wouldn't let me upload it.

Edit -

OK I figured out how to do this. As the GridView was being populated I used the event OnRowCreated 开发者_Python百科to then set the text of the field I required. To get to the control I needed I used the e.Row.FindControl.

The code for it was pretty simple in the end. I always seem to figure this stuff out when I finally ask for help.

 try
  {
    int orderID = e.Row.RowIndex;

    Order order = ShopEntities.Orders.Single(o => o.OrderStateID == orderID);
    // I can now get the list of orderstates
    OrderStateDefinition osd = order.OrderStates.OrderBy(o => o.Date).Last().OrderStateDefinition;

    ((Label)e.Row.FindControl("Label2")).Text = osd.State;
  }
  catch
  {
  }


I often find it's easier to create your own SQL that does this. the sql might be a little complex, but it's easier than mucking with the c#.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜