473,511 Members | 9,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2790
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
281
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 (frmData). In the main form I have created: Private...
0
2321
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 interface (UI) elements with a data source to generate a...
16
2989
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 up with is by creating typed (.xsd) datasets. For...
1
3919
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) Small files (0 < length < 10 mb), containing...
1
2589
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 to do the same without any UI being involved at...
0
1701
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 information from a number of different data...
10
1896
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 techniques like data binding, data adapter wizards etc...
2
4886
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 details. My data binding (a DataTable bound to...
19
2206
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 the marketing speak goes), 'create database...
14
14602
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 give the DataSourceId and in the custom-control...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.