Wilson Kutegeka

Microsoft MVP - Visual Basic www.clinicmaster.net

May 2007 - Posts

ComboBox: Popular and Stubborn

To most of my windows applications, combobox is probably second only to textbox in terms of popularity as a data capture control. That being said, combobox has been the most frustrating control for me to work with.

When a combobox is populated as shown below

Me.ComboBox1.DataSource = data
Me.ComboBox1.DisplayMember = "Description"
Me.ComboBox1.ValueMember = "Code"

  •  If one wants to reset the combobox with say (Me.ComboBox1.SelectedIndex = -1) clearance is not guaranteed. You need to use

           Me.ComboBox1.SelectedIndex = -1
           Me.ComboBox1.SelectedIndex = -1

  • If you set the sorted property of the combobox to true, selected value and displayed member won’t be in sync.
  • If you bind to a DataGridViewComboBoxColumn, from the same source that binds to the combobox, and make a selection from the DataGridViewComboBoxColumn, the same value is automatically selected in the combobox.

The documentation

 http://msdn2.microsoft.com/en-us/library/kh56dwh6.aspx

from MSDN says that Windows Forms controls do not sort when they are data-bound. However, my internal tests show that Combobox sorts only the Display Member.

No problem if source is sorted though

MS SQL Server Decimal datatype Vs .NET’s System.Decimal datatype

In my applications, some users want money rounded to two places of decimal (e.g 5,692.25) at times they want no decimal place at all (e.g 5,692). It occurred to me that one day, some one might request for four places of decimal. Thus I needed a way to automatically format DataGridView among other controls at run time.

I did write some code such as

           

            ''' <summary>

            ''' All the grid columns of a supplied datatype will be formated to the supplied style

            ''' </summary>

            ''' <param name="grid"></param>

            ''' <param name="style"></param>

            ''' <param name="dataType"></param>

            ''' <remarks></remarks>

            Public Sub FormatGridColumn(ByVal grid As DataGridView, ByVal style As DataGridViewCellStyle, ByVal dataType As Type)

 

                Dim columnStyle As New DataGridViewCellStyle()

                Dim columnType As Type

 

                Try

 

                    columnStyle.Alignment = DataGridViewContentAlignment.MiddleRight

                    columnStyle.Format = "#,##0.00"

 

                    columnType = GetType(Decimal)

 

                    If IsNothing(dataType) = False Then columnType = dataType

                    If IsNothing(style) = False Then columnStyle = style

 

                    For Each column As DataGridViewColumn In grid.Columns

                        If column.ValueType Is Nothing Then Continue For

                        If column.ValueType.FullName.Equals(columnType.FullName) Then

                            grid.Columns(column.Name).DefaultCellStyle.ApplyStyle(columnStyle)

                        End If

                    Next

 

                Catch ex As Exception

                End Try

 

            End Sub


On inspecting corresponding datatypes of a databound DataGridView, found out that both Currency and Decimal datatypes of MS SQL map to System.Decimal. Well we all know that System.Decimal is much more accurate than Double or Single/Float but am not sure if a datatype such as Decimal (5, 2)(SQL) should map to Decimal (VB).

Why?

For a column that stores an hourly wage, a DECIMAL with a precision of 5 and scale of 2 i.e. Decimal (5, 2) is probably a better choice than MONEY or SMALLMONEY though I’d use the latter types for wage. Both of the latter types have far too much precision and scale to be useful for a value that is almost always going to be less than $60.00

Precision is the number of decimal places that the column can take; scale is the number of decimal places allowed to the right of the decimal point. So, if you need to store percentages (e.g. 28.67%) you would use DECIMAL (5, 2).  Assuming you needed to cover the 100.00% case. If you wanted more accuracy, you could use DECIMAL (8, 5), which would allow a value like 38.96523%.

Now if you asked me as what VB should do to such a datatype, I’d say inspect precision and scale and there after convert accordingly to Single or Double or Decimal but not always.

Wilson Kutegeka | Microsoft MVP - Visual Basic | C#
Developer | Promoter | ClinicMaster Software
web
www.clinicmaster.net

 

My Favorite Programming Language

When I switched to developing computer applications using Microsoft .NET technology, the question was which of the two languages in the dot net framework should I go for VB .NET or C #? Well I did choose VB .NET since I was coming from the VB background. However wherever I would go (Including the Internet) for sample code etc at that time, I’d find most of the code in C#, easy for me to read and interpret C# and I must admit that up to now I am more of a reader than a writer in C#. Then the question came, should I continue with VB .NET or go for C# but when I looked for what can be done in C# and not in VB .NET, I could not see it apart from XML comments and perhaps operator overloading at the time, which I didn’t need that much by then, I could not see the big difference apart from just the syntax.

Conclusion

Whether VB .NET or C#, feel free to go with your own choice if you asked me, I’d say VB .NET you know every one including Microsoft is with C#. So why should we follow them.