|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
A DataGridView question
Visual Studio 2008
Displaying a dataset in a DataGridView. Some columns of the dataset contain numeric values (integers) For example 4 in the database might mean "Oil" and 5 might mean "Electricity" I want to display "Oil" or "Electricity" based on the value of the field in the DataGridView and not the numeric value. What is a simple way to do this? |
|
#2
|
|||
|
|||
|
Use the DataGridViewComboBoxColumn type in your DataGridView.
Set the DataSource to a query that retrieves the ID and Description from the lookup table in the database. Set the DisplayMember property to the column name of the Description column, and set the ValueMember property to the column name of the ID column. |
|
#3
|
|||
|
|||
|
Thanks for that answer Maddogbrown.
The problem is that there was quite a few fields that I need to do this on and the look up texts are fixed. Its the input from a survey, I didnt really want to build a lookup table for every column. I have solved the problem I think by using the Cell_Format even handler as follows. Private Sub MyTableDataGridView_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles MyTableDataGridView.CellFormatting Dim i As IntegerEndSub This seems to work very well and I can even change to a picture or any other format just by altering the column type. Best wishes Steve |
|
#4
|
|||
|
|||
|
Quote:
Oh god no, don't do it that way; your application shouldn't have to deal with with mapping integer keys to their descriptions. More specifically, your application layer is now acting as a "container" for the mappings (because of the hardcoded descriptions) Use the CASE in your SQL query and be done with it: Code:
SELECT CASE EnergyType WHEN 1 THEN 'GAS' WHEN 2 THEN 'LPG' END as EnergyTypeDescr From Table Last edited by MadDogBrown : June 18th, 2009 at 08:36 AM. |
|
#5
|
|||
|
|||
|
Sorry for the delay in replying but I have had to have a play with what you suggest and get my head round it.
First - I have Never seen a Case When statement used that way in SQL in all my years. (Quite a few ) I have never seen any such example.Always thought it was a failng of SQL - ignorance is a great excuse..... not - as they say.I simply never understood that was possible and I am always willing to learn. I have tried you method and have to say it works superbly well. Thanks for the help. Seriously - thanks! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > A DataGridView question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|