472,325 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Data Binding Question

Think it is great the way that you can set up a datsource, value member, and
display member for a combobox, and map a 'code' to a 'description' and back
again by binding the combobox to a datasource that contains the code. Now
suppose that you want it to be read-only? That is, you have the 'code' in
your DB, want the associated 'description' to display, but not let the user
change it on this particular form. There is no read-only property for a
combobox like with a textbox, and there are no datasource, display member,
value member properties for a textbox or label controls. Is there some easy
way to accomplish this? The only way I can think to do this is to use the
binding class 'format' event to lookup the description myself. Any other
ideas?

--
Terry
Nov 5 '07 #1
9 2666
Terry,

I thought that it was easily just this one, however I am in doubt.

Dropdownlist

http://msdn2.microsoft.com/en-us/lib...le(VS.71).aspx

Cor

Nov 5 '07 #2
The user can still change the selected index. And while I could change the
'Data Source Update Mode' (under advanced binding) to Never, this would be
confusing. Really want to show it in a label or readonly text box so the
user is aware that he/she can not change it (here).
--
Terry
"Cor Ligthert[MVP]" wrote:
Terry,

I thought that it was easily just this one, however I am in doubt.

Dropdownlist

http://msdn2.microsoft.com/en-us/lib...le(VS.71).aspx

Cor

Nov 5 '07 #3
Terry,

Why are you then binding it, just fill the itemarray with the fields you
want and you are ready.

This needs really only the most simple for each loop there is.

Cor

Nov 6 '07 #4
Hi Terry,

If we could custom draw the input box part of a ComboBox as well as its
items in the drop down list, a possible workaround may be custom drawing
the ComboBox to get what you want.

Unfortunately, we can not custom draw the input box part of a ComboBox.

IMO, the solution to use the Binding class's Format event to look up the
description youself you have mentioned in your first reply is good. In
addition, I think it would be better to create a custom control to
encapsulate the above logic. Then you could use this custom control
wherever you want.

The following is a sample of this custom control. Note that you shoud set
the DataSource, ValueMember and DisplayMember properties of the derived
Label before bind the Text property of the control to a data source.

Public Class MyLabel
Inherits Label

Private _datasource As IList
Private _valuemember As String
Private _displaymember As String

Public Property DataSource() As IList
Get
Return _datasource
End Get
Set(ByVal value As IList)
_datasource = value
End Set
End Property

Public Property ValueMember() As String
Get
Return _valuemember
End Get
Set(ByVal value As String)
_valuemember = value
End Set
End Property

Public Property DisplayMember() As String
Get
Return _displaymember
End Get
Set(ByVal value As String)
_displaymember = value
End Set
End Property

Sub New()
AddHandler Me.DataBindings.CollectionChanged, AddressOf
DataBindings_CollectionChanged
End Sub

Private Sub DataBindings_CollectionChanged(ByVal sender As Object,
ByVal e As System.ComponentModel.CollectionChangeEventArgs)
Dim b As Binding = CType(e.Element, Binding)
If (Not (b Is Nothing)) Then
If (b.PropertyName = "Text") Then
If (e.Action = CollectionChangeAction.Add) Then
AddHandler b.Format, AddressOf b_Format
b.ReadValue()
ElseIf (e.Action = CollectionChangeAction.Remove) Then
RemoveHandler b.Format, AddressOf b_Format
End If
End If
End If
End Sub

Private Sub b_Format(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If (Not (_datasource Is Nothing) And _datasource.Count 0) Then
Dim pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(_datasource(0))
For i As Integer = 0 To _datasource.Count - 1
If (pdc(_valuemember).GetValue(_datasource(i)).ToStri ng() =
e.Value.ToString()) Then
e.Value = pdc(_displaymember).GetValue(_datasource(i))
Exit For
End If
Next
Else
Throw New Exception("Please set the DataSource, DisplayMember
and ValueMember properties first")
End If
End Sub

End Class

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 6 '07 #5
Linda,

That was my first thought too, but is this not a little bit long solution
while you have direct the result by filling the itemarray?

:-)

Cor

Nov 6 '07 #6
Hi Linda,
I was able to accomplish what I wanted by using the Format event. As a
learnig exercise for myself, I am going to try the custom control aproach
that you have suggested here. I have never done this before and have a few
general questions for you. I realize I could just add the code you have
given me here as a new class in the project and then reference it through
code in my form. What approach do I take to make it available to other
projects? Should I build a seperate project for it? If so, is it a class
library or a windows control project? How would I get it available in the
toolbox so that I can drop and position it on my form? Maybe you could point
me to something I could read.
Thanks for all your help!
--
Terry
"Linda Liu[MSFT]" wrote:
Hi Terry,

If we could custom draw the input box part of a ComboBox as well as its
items in the drop down list, a possible workaround may be custom drawing
the ComboBox to get what you want.

Unfortunately, we can not custom draw the input box part of a ComboBox.

IMO, the solution to use the Binding class's Format event to look up the
description youself you have mentioned in your first reply is good. In
addition, I think it would be better to create a custom control to
encapsulate the above logic. Then you could use this custom control
wherever you want.

The following is a sample of this custom control. Note that you shoud set
the DataSource, ValueMember and DisplayMember properties of the derived
Label before bind the Text property of the control to a data source.

Public Class MyLabel
Inherits Label

Private _datasource As IList
Private _valuemember As String
Private _displaymember As String

Public Property DataSource() As IList
Get
Return _datasource
End Get
Set(ByVal value As IList)
_datasource = value
End Set
End Property

Public Property ValueMember() As String
Get
Return _valuemember
End Get
Set(ByVal value As String)
_valuemember = value
End Set
End Property

Public Property DisplayMember() As String
Get
Return _displaymember
End Get
Set(ByVal value As String)
_displaymember = value
End Set
End Property

Sub New()
AddHandler Me.DataBindings.CollectionChanged, AddressOf
DataBindings_CollectionChanged
End Sub

Private Sub DataBindings_CollectionChanged(ByVal sender As Object,
ByVal e As System.ComponentModel.CollectionChangeEventArgs)
Dim b As Binding = CType(e.Element, Binding)
If (Not (b Is Nothing)) Then
If (b.PropertyName = "Text") Then
If (e.Action = CollectionChangeAction.Add) Then
AddHandler b.Format, AddressOf b_Format
b.ReadValue()
ElseIf (e.Action = CollectionChangeAction.Remove) Then
RemoveHandler b.Format, AddressOf b_Format
End If
End If
End If
End Sub

Private Sub b_Format(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If (Not (_datasource Is Nothing) And _datasource.Count 0) Then
Dim pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(_datasource(0))
For i As Integer = 0 To _datasource.Count - 1
If (pdc(_valuemember).GetValue(_datasource(i)).ToStri ng() =
e.Value.ToString()) Then
e.Value = pdc(_displaymember).GetValue(_datasource(i))
Exit For
End If
Next
Else
Throw New Exception("Please set the DataSource, DisplayMember
and ValueMember properties first")
End If
End Sub

End Class

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 8 '07 #7
Hi Terry,

About your first question, you could add some attributes on the DataSource,
DisplayMember and ValueMember properties of the custom control to make the
design time behavior of these three properties like that of the DataSource,
DisplayMember and ValueMember properties of a ComboBox.

The following is a sample:

Imports System.Drawing.Design

Public Class MyLabel
Inherits Label
<DefaultValue(CType(Nothing, String)),
AttributeProvider(GetType(IListSource))_
Public Property DataSource() As IList
...
End Property

<Editor("System.Windows.Forms.Design.DataMemberFie ldEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", _
GetType(UITypeEditor)), DefaultValue("")_
Public Property ValueMember() As String
...
End Property

<DefaultValue(""), _
TypeConverter("System.Windows.Forms.Design.DataMem berFieldConverter,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"),
Editor("System.Windows.Forms.Design.DataMemberFiel dEditor, System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
GetType(UITypeEditor))_
Public Property DisplayMember() As String
...
End Property
...
End Class

Build the project and open the form containing the custom control in the
designer. Select the custom control on the form and go to the DataSource
entry in the Properties window, a drop down arrow should appear on the
right hand. If you click it, a drop down window should open with all data
sources available in the project listed in it. Then go to the DisplayMember
or ValueMember entry in the Properties window and click the drop down arrow
on the right, and you should see a drop down window opening with all
properites available in the selected data source.

About your second question, are all the objects in the data source of the
same type? If the problem is still not solved, please send me a simple
project that could just reproduce the problem. To get my actual email
address, remove 'online' from my displayed email address.

Sincerely,
Linda Liu
Microsoft Online Community Support

Nov 21 '07 #8
Hi Terry,

Thank you for your reply!

I understand your second question now.

Firstly, the reason why " Nothing = "" " returns true is that string
comparisons treat Nothing as "" (an empty string).

Secondly, it seems that the internal implementation of the ComboBox regards
Nothing equal to DBNull.Value.

So we can modify the code in the b_Format method in the derived Label class
as follows to get the same behavior of the ComboBox:

Public Class MyLabel
Inherits Label
...
Private Sub b_Format(ByVal sender As Object, ByVal e As ConvertEventArgs)
If (Not (_datasource Is Nothing) And _datasource.Count 0) Then
Dim pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(_datasource(0))
For i As Integer = 0 To _datasource.Count - 1
Dim valueinDS As Object =
pdc(_valuemember).GetValue(_datasource(i))
If ((valueinDS Is Nothing Or valueinDS Is DBNull.Value) And
(e.Value Is Nothing Or e.Value Is DBNull.Value)) Then
e.Value = pdc(_displaymember).GetValue(_datasource(i))
Exit For
ElseIf (valueinDS IsNot Nothing And valueinDS IsNot
DBNull.Value And e.Value IsNot Nothing And e.Value IsNot DBNull.Value) Then
If (valueinDS.ToString() = e.Value.ToString()) Then
e.Value =
pdc(_displaymember).GetValue(_datasource(i))
Exit For
End If
End If
Next
Else
Throw New Exception("Please set the DataSource, DisplayMember
and ValueMember properties first")
End If
End Sub

End Class

Please try it in your project to see if it solves the problem and let me
know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

Nov 22 '07 #9
Hi Linda,
Thanks! Works like a charm! I did make one small change, which I will
share...
If (Not (_datasource Is Nothing) AndAlso _datasource.Count 0) Then
| so the second half
wont be evaluated if nothing

If you are in the US - Happy Thanksgiving! If not, and in any case - Have a
Great Day and thanks again for all your help!

--
Terry
"Linda Liu[MSFT]" wrote:
Hi Terry,

Thank you for your reply!

I understand your second question now.

Firstly, the reason why " Nothing = "" " returns true is that string
comparisons treat Nothing as "" (an empty string).

Secondly, it seems that the internal implementation of the ComboBox regards
Nothing equal to DBNull.Value.

So we can modify the code in the b_Format method in the derived Label class
as follows to get the same behavior of the ComboBox:

Public Class MyLabel
Inherits Label
...
Private Sub b_Format(ByVal sender As Object, ByVal e As ConvertEventArgs)
If (Not (_datasource Is Nothing) And _datasource.Count 0) Then
Dim pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(_datasource(0))
For i As Integer = 0 To _datasource.Count - 1
Dim valueinDS As Object =
pdc(_valuemember).GetValue(_datasource(i))
If ((valueinDS Is Nothing Or valueinDS Is DBNull.Value) And
(e.Value Is Nothing Or e.Value Is DBNull.Value)) Then
e.Value = pdc(_displaymember).GetValue(_datasource(i))
Exit For
ElseIf (valueinDS IsNot Nothing And valueinDS IsNot
DBNull.Value And e.Value IsNot Nothing And e.Value IsNot DBNull.Value) Then
If (valueinDS.ToString() = e.Value.ToString()) Then
e.Value =
pdc(_displaymember).GetValue(_datasource(i))
Exit For
End If
End If
Next
Else
Throw New Exception("Please set the DataSource, DisplayMember
and ValueMember properties first")
End If
End Sub

End Class

Please try it in your project to see if it solves the problem and let me
know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

Nov 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form...
0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come...
1
by: Patrick | last post by:
Hi all, New to .NET, I'm working on an Winforms client application using VS 2005 beta2. My needs considering data storage are the followings: ...
1
by: matty.hall | last post by:
There's a lot of information out there about data-binding UI objects (i.e. derived from Control) to non-UI custom business objects. Is it possible...
0
by: popsovy | last post by:
Hi I have a question about whether Data Binding can facilitate the process of saving data in a web application I learned that you can data bind...
10
by: Fares Eidi | last post by:
I am a VB6 intermediate programmer just starting out using VB.net and would like to ask what I'd be missing out on if I just use time saving...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.