471,616 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Combobox - Format Items

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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #1
5 3503
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" <ca*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 21 '05 #2
Carrie,

Good evening,

You need for that the datatable.databinding events

http://msdn.microsoft.com/library/de...ventsTopic.asp

I hope this helps,

Cor
Nov 21 '05 #3
Carrie,

My answer was wrong, you can add an extra datacolumn with an expression to
your datatable

http://msdn.microsoft.com/library/de...ctortopic4.asp

I hope that this one helps?

Cor
Nov 21 '05 #4
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.

"Carrie" wrote:
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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 21 '05 #5
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" <ca*******@hotmail-dot-com.no-spam.invalid> escribi en el mensaje
news:42********@127.0.0.1...
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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Newbee | last post: by
reply views Thread by Carrie | last post: by
reply views Thread by Doug | last post: by
2 posts views Thread by pmcguire | last post: by
5 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
4 posts views Thread by Jerad Rose | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.