开发者

Display related data in a datagridviewtextboxcolumn

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 val开发者_StackOverflowue may be 3 but I want to display "Furnace 5" in the datagridview instead of 3 (which it is now displaying).


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜