Combobox - Format Items

C

Carrie

Good Morning,

I have a combobox whose list is bound to DataSet1.Sales Growth
Options.Sales Growth. The data is double and I would like it
displayed as a percentage. I have tried two things:

1. I bound the data using DataSource and DisplayMember and then used
the following code:

Private Sub FormatPercent(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If TypeOf e.Value Is Double Then
e.Value = CType(e.Value, Double).ToString("p")
End If
End Sub

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
AddHandler Me.SalesGrowthCombo.DataBindings("Text").Format, AddressOf
Me.FormatNumberWithDecimals

The result is that the first item in the list was formatted correctly,
but
the drop-down list of items when you pressed the arrow wasn't.

2. Then I tried eliminating the DataSource and DisplayMember values
and replacing it with the following:

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim SalesGrowthItems As New Binding("Text", DataSet1, "Growth
Rate Options.Growth Rate")
Add Handler SalesGrowthItems.Format, AddressOf Me.FormatPercent

This resulted in only one item in my combobox. This one item was
formatted.

If anybody could give me assistance, I would appreciate it.

Thanks,

Carrie




Expand AllCollapse All
 
C

Chris, Master of All Things Insignificant

Two solution as I see it.

1: Add a new column in your dataset that contains the formatted text you
want in the combobox. (If you populate the Dataset from SQL, this is very
easy to do)
2: Loop through the rows in the dataset and add them to the combo box
yourself, formatting as you go.

Both are easy to do. If you need more help, just let us know.

Chris

Carrie said:
Good Morning,

I have a combobox whose list is bound to DataSet1.Sales Growth
Options.Sales Growth. The data is double and I would like it
displayed as a percentage. I have tried two things:

1. I bound the data using DataSource and DisplayMember and then used
the following code:

Private Sub FormatPercent(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If TypeOf e.Value Is Double Then
e.Value = CType(e.Value, Double).ToString("p")
End If
End Sub

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
AddHandler Me.SalesGrowthCombo.DataBindings("Text").Format, AddressOf
Me.FormatNumberWithDecimals

The result is that the first item in the list was formatted correctly,
but
the drop-down list of items when you pressed the arrow wasn't.

2. Then I tried eliminating the DataSource and DisplayMember values
and replacing it with the following:

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim SalesGrowthItems As New Binding("Text", DataSet1, "Growth
Rate Options.Growth Rate")
Add Handler SalesGrowthItems.Format, AddressOf Me.FormatPercent

This resulted in only one item in my combobox. This one item was
formatted.

If anybody could give me assistance, I would appreciate it.

Thanks,

Carrie




Expand AllCollapse All
 
G

Guest

I use the method Chris proposed all the time. As he said it's easy. Give it
a try and let us know the results.
 
V

VBen

I've used Format and Parse events to even format a picture, so it handles
DBNull values, and displays a predefined "no picture" picture. This is the
most ".net" approach, since .net is an event-oriented platform, and it gets
your code the most clean.
In the Format event, you receive each value, and you format it to display in
the control. In the Parse event, you "un-format" the value to save it to the
database. You only have to transform these values, and don't have to worry
for which rows are shown. This is done by the binding.
I suggest this approach.
Hope this helps.
VBen.

Carrie said:
Good Morning,

I have a combobox whose list is bound to DataSet1.Sales Growth
Options.Sales Growth. The data is double and I would like it
displayed as a percentage. I have tried two things:

1. I bound the data using DataSource and DisplayMember and then used
the following code:

Private Sub FormatPercent(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If TypeOf e.Value Is Double Then
e.Value = CType(e.Value, Double).ToString("p")
End If
End Sub

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
AddHandler Me.SalesGrowthCombo.DataBindings("Text").Format, AddressOf
Me.FormatNumberWithDecimals

The result is that the first item in the list was formatted correctly,
but
the drop-down list of items when you pressed the arrow wasn't.

2. Then I tried eliminating the DataSource and DisplayMember values
and replacing it with the following:

Private Sub CashFlow1Form_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim SalesGrowthItems As New Binding("Text", DataSet1, "Growth
Rate Options.Growth Rate")
Add Handler SalesGrowthItems.Format, AddressOf Me.FormatPercent

This resulted in only one item in my combobox. This one item was
formatted.

If anybody could give me assistance, I would appreciate it.

Thanks,

Carrie




Expand AllCollapse All
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top