1

I have a datagridview bound to a bindingsource. Two of the columns contain values that link to the primary keys in other tables. Instead of displaying the primary key values, I would like to display the name of the item associated with the primary key. In other words, the primary key value may be 3 but I want to display "Furnace 5" in the datagridview instead of 3 (which it is now displaying).

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
blueshift
  • 831
  • 2
  • 12
  • 24

1 Answers1

1

There are at least four ways of doing this (from easiest to hardest)

1) Linq to DataSet This assumes a typed DataSet with two DataTables Products and ProductTypes. This isn't required (they don't have to be in the same dataset and the ProductType doesn't have to be DataTable)

using linq to dataset requires .NET 3.5 and up

Dim ProductList = _
            (From product In p.products.AsEnumerable() _
            Join types In p.productTypes.AsEnumerable() _
            On product.ProductTypeID Equals types.ProductTypeID _
            Select New With {
                .ProductId = product.ProductID, _
                .Description = product.Description, _
                .Type = types.Description}).ToList

        Me.DataGridView1.DataSource = ProductList

2) Retrieve the data the way you need it and bind to the desired column

3) Don't use a TextBoxColumn and use a ComboBoxDataColumn instead.

4) Create a custom DataGridViewColumn that does the lookup.

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155