Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to display the lookup value in a DataGridViewComboBox in view mode
 

How to display the lookup value in a DataGridViewComboBox in view mode

When a DataGridViewComboBoxCell is not in edit mode, the value displayed in the cell is from the DataPropertyName I guess. However, I would like to display the looked up value.

What I want to attain is that when user looks at the grid, without editing any combobox cells, then the looked up values should be displayed in all of those combobox cells. Usually I would have a lookup table in my database; with an integer column and a string column. Each integer corresponds to a string value. My users won't understand a column full of integers. Instead they would understand the looked up values.

Is this possible out of the box, or do I have to create a special DataGridViewComboBox?
claesbrandt  Thursday, December 15, 2005 2:53 PM

The combobox column supports this. Just set the DisplayMember and ValueMember. Here are some steps:

 

1) In VS created a new winforms app

2) Added a new datasource to your database

3) Select your products table and your "Lookup" table in the datasource wizard.

4) After the wizard is done, drag and drop the products table in the DataSources window to your form to create the initial DataGridView and binding sources

5) Next right clicked the DGV and goto Edit columns dialog

6) Find the "lookup id" column and goto the Column Type property. Change that to be of type ComboBoxColumn.

7) Next, still for the same column, goto the DataSource property and select the "Lookup" table.

8) Next set the column's DisplayMember property to the database column that has the string names.

9) And then set the column's ValueMember property to the database column that has the ids

10) Compiled and run

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�o:p>

 

Mark Rideout  Thursday, December 15, 2005 5:55 PM

The combobox column supports this. Just set the DisplayMember and ValueMember. Here are some steps:

 

1) In VS created a new winforms app

2) Added a new datasource to your database

3) Select your products table and your "Lookup" table in the datasource wizard.

4) After the wizard is done, drag and drop the products table in the DataSources window to your form to create the initial DataGridView and binding sources

5) Next right clicked the DGV and goto Edit columns dialog

6) Find the "lookup id" column and goto the Column Type property. Change that to be of type ComboBoxColumn.

7) Next, still for the same column, goto the DataSource property and select the "Lookup" table.

8) Next set the column's DisplayMember property to the database column that has the string names.

9) And then set the column's ValueMember property to the database column that has the ids

10) Compiled and run

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�o:p>

 

Mark Rideout  Thursday, December 15, 2005 5:55 PM

Thanks for the reply Mark,

However, I found the reason to be something else. When you have the dataset on the same form as the datagrid, everything works as described in the documentation and as you said. But when assigning the dataset runtime, the combobox's properties has to be reassigned, after the dataset has been assigned.

Now all works fine, thanks for the answer.

claesbrandt  Friday, December 16, 2005 11:27 AM

You can use google to search for other answers

Custom Search

More Threads

• Writing to and Reading from Access Database Problem?
• DataGrid - user edits
• problems in adding rows to datagrid
• Database Objects, Datasets, and Datagrids
• Displaying Database information in ListView
• Multiple BindingSources for the same table
• Collection was modified exception while doing .DataTable.Select
• Problem with Sum(child(relationName).field)
• need help with custom control
• Reparenting of databound controls clears data binding ?