locked
DataTable Foreign Key value replacement RRS feed

  • Question

  • Hi there. I am a novice programmer so please bear with me. I am trying to databind a DataTable on a datagrid that has a foreign key that references another DataTable in a DataSet. The simplified scenario is as follows:

    The 1st table, CarTable, has an "CarID" (primary key) column, a "CarName" column, a "YearModel" column and a "ManufacturerID" column (foreign key)

    The 2nd table, ManufacturerTable has 2 columns, namely "ManufacturerID" (primary key) and "ManufacturerName".

    I want to bind the 1st table, namely CarTable, to a DataGridView control on a form. This is not an issue, however I would like to display the ManufacturerName instead of the ManufacturerID in the one column of the DataGridView. Is this possible? Would I have to create another table by manually looping through the original tables to create a combination of the two.

     

    Thursday, March 31, 2011 2:17 PM

Answers

  • setup your sql query as "Select Car.CarId,Car.CarName, Car.YearModel, Manufacturer.ManufactureName from Car "+
                                         " join Manufacturer on Manufacturer.ManufacdturerID = Car.ManufacturerID";
     "CharlH" wrote in message news:d7b1a670-8083-48ee-bdc7-ad19e7b9bec6@communitybridge.codeplex.com...

    Hi there. I am a novice programmer so please bear with me. I am trying to databind a DataTable on a datagrid that has a foreign key that references another DataTable in a DataSet. The simplified scenario is as follows:

    The 1st table, CarTable, has an "CarID" (primary key) column, a "CarName" column, a "YearModel" column and a "ManufacturerID" column (foreign key)

    The 2nd table, ManufacturerTable has 2 columns, namely "ManufacturerID" (primary key) and "ManufacturerName".

    I want to bind the 1st table, namely CarTable, to a DataGridView control on a form. This is not an issue, however I would like to display the ManufacturerName instead of the ManufacturerID in the one column of the DataGridView. Is this possible? Would I have to create another table by manually looping through the original tables to create a combination of the two.

    • Marked as answer by CharlH Friday, April 1, 2011 8:12 AM
    Thursday, March 31, 2011 3:04 PM
  • Or add an extra column to your dataset and use the Expression property to fetch the data from the referenced table:

    http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=VS.100).aspx

    • Marked as answer by CharlH Friday, April 1, 2011 8:12 AM
    Thursday, March 31, 2011 4:02 PM

All replies

  • setup your sql query as "Select Car.CarId,Car.CarName, Car.YearModel, Manufacturer.ManufactureName from Car "+
                                         " join Manufacturer on Manufacturer.ManufacdturerID = Car.ManufacturerID";
     "CharlH" wrote in message news:d7b1a670-8083-48ee-bdc7-ad19e7b9bec6@communitybridge.codeplex.com...

    Hi there. I am a novice programmer so please bear with me. I am trying to databind a DataTable on a datagrid that has a foreign key that references another DataTable in a DataSet. The simplified scenario is as follows:

    The 1st table, CarTable, has an "CarID" (primary key) column, a "CarName" column, a "YearModel" column and a "ManufacturerID" column (foreign key)

    The 2nd table, ManufacturerTable has 2 columns, namely "ManufacturerID" (primary key) and "ManufacturerName".

    I want to bind the 1st table, namely CarTable, to a DataGridView control on a form. This is not an issue, however I would like to display the ManufacturerName instead of the ManufacturerID in the one column of the DataGridView. Is this possible? Would I have to create another table by manually looping through the original tables to create a combination of the two.

    • Marked as answer by CharlH Friday, April 1, 2011 8:12 AM
    Thursday, March 31, 2011 3:04 PM
  • Or add an extra column to your dataset and use the Expression property to fetch the data from the referenced table:

    http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=VS.100).aspx

    • Marked as answer by CharlH Friday, April 1, 2011 8:12 AM
    Thursday, March 31, 2011 4:02 PM