Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2009, 04:09 AM
sphughes sphughes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 35 sphughes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 43 m 5 sec
Reputation Power: 2
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?

Reply With Quote
  #2  
Old June 17th, 2009, 02:33 PM
MadDogBrown MadDogBrown is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Posts: 730 MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 40 m 5 sec
Reputation Power: 516
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.

Reply With Quote
  #3  
Old June 18th, 2009, 02:19 AM
sphughes sphughes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 35 sphughes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 43 m 5 sec
Reputation Power: 2
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 Integer
If e.ColumnIndex = 5 Then
If e.Value IsNot DBNull.Value Then i = e.Value Else i = 0
Select Case i
Case 1
e.Value = "Gas"
Case 2
e.Value = "LPG"
Case 3
e.Value = "Oil"
Case Else
e.Value = ""
End Select
End If
EndSub

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

Reply With Quote
  #4  
Old June 18th, 2009, 08:31 AM
MadDogBrown MadDogBrown is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Posts: 730 MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 40 m 5 sec
Reputation Power: 516
Quote:
Originally Posted by sphughes
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 Integer
If e.ColumnIndex = 5 Then
If e.Value IsNot DBNull.Value Then i = e.Value Else i = 0
Select Case i
Case 1
e.Value = "Gas"
Case 2
e.Value = "LPG"
Case 3
e.Value = "Oil"
Case Else
e.Value = ""
End Select
End If
EndSub

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

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.

Reply With Quote
  #5  
Old June 19th, 2009, 07:55 PM
sphughes sphughes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 35 sphughes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 43 m 5 sec
Reputation Power: 2
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > A DataGridView question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT