many-to-one relationships

Are many-to-one relationships possilble with ADO.Net datasets

I have an orders table in a datagrid. The orders table contains a customer ID field. As the user scrolls the table, I would like to have a textbox outside the grid reflect the customer NAME field from the customer table. This is sort of a classic lookup concept, but I can't seem to figure out how to implement it.

As usual, any help is appreciated.


Answer this question

many-to-one relationships

  • RCroft


    Both tables should be on the same dataset, then add a realtion

    ds.Relationships.Add(new DataRelation("CustomerOrders",ds.Tables["customers"].Columns["customerID"],ds.Tables["orders"].Columns["customerID"]))

    then use the GetParentRow method of the selected row in the orders table to get the customer row and change the textox:

    DataRow dr = selectedRow.GetParentRow("CustomerOrders")

    TextBox.Text = dr["CustomerName"]

    CostumerOrders is the name of the realtion.
    I'm not writing the code to get the selectedRow but you shouldn't have trouble with that.

    maybe is there a better way to do this in windowsforms, I'm not sure as I'm more oriented to ASP.NET coding.

  • many-to-one relationships