BindingSource控件
BindingSource控件
BindingSource控件
BindingSource控件是.NET Compact Framework 2.0提供的新控件之一。BindingSource控件与数据源建立连接,然后将窗体中的控件与BindingSource控件建立绑定关系来实现数据绑定,简化数据绑定的过程。BindingSource控件即是一个连接后台数据库的渠道,同时又是一个数据源,因为BindingSource控件即支持向后台数据库发送命令来检索数据,又支持直接通过BindingSource控件对数据进行访问、排序、筛选和更新操作。BindingSource控件能够自动管理许多绑定问题。BindingSource控件没有运行时界面,无法在用户界面上看到该控件。
BindingSource控件通过Current属性访问当前记录,通过List属性访问整个数据表。表4-8列出了BindingSource控件的主要属性。
表4-8:BindingSource的主要属性
属性
|
说明
|
AllowEdit
|
指示是否可以编辑
BindingSource
控件中的记录。
|
AllowNew
|
指示是否可以使用
AddNew
方法向
BindingSource
控件添加记录。
|
AllowRemove
|
指示是否可从
BindingSource
控件中删除记录。
|
Count
|
获取
BindingSource
控件中的记录数。
|
CurrencyManager
|
获取与
BindingSource
控件关联的当前记录管理器。
|
Current
|
获取
BindingSource
控件中的当前记录。
|
DataMember
|
获取或设置连接器当前绑定到的数据源中的特定数据列表或数据库表。
|
DataSource
|
获取或设置连接器绑定到的数据源。
|
Filter
|
获取或设置用于筛选的表达式。
|
Item
|
获取或设置指定索引的记录。
|
Sort
|
获取或设置用于排序的列名来指定排序。
|
通过Current属性及RemoveCurrent、EndEdit、CancelEdit、Add和AddNew方法可实现对当前记录的编辑操作。表4-3列出了BindingSource控件的主要方法。
表4-3:BindingSource的主要方法
方法
|
说明
|
Add
|
将现有项添加到内部列表中。
|
CancelEdit
|
取消当前编辑操作。
|
Clear
|
从列表中移除所有元素。
|
EndEdit
|
将挂起的更改应用于基础数据源。
|
Find
|
在数据源中查找指定的项。
|
MoveFirst
|
移至列表中的第一项。
|
MoveLast
|
移至列表中的最后一项。
|
MoveNext
|
移至列表中的下一项。
|
MovePrevious
|
移至列表中的上一项。
|
RemoveCurrent
|
从列表中移除当前项。
|
1.1.1 绑定操作
清单4-11演示通过BindingSource控件将包含TreeNode列表的BindingList对象绑定到ListBox控件。清单4-12演示包含TreeNode列表的BindingList对象的类定义。
清单 4-11 使用BindingSource控件进行绑定
……
Dim myNodeList As New MyTreeNodeList()
myNodeList.Add(New Windows.Forms.TreeNode("
西藏
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广西壮族自治区
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
四川省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
河南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
陕西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
湖南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
吉林省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
宁夏回族自治区
"))
BindingSource1.DataSource = myNodeList
ListBox1.DataSource = BindingSource1
ListBox1.DisplayMember = "Text"
……
|
清单 4-12 BindingList对象的类定义
Public Class MyTreeNodeList
Inherits System.ComponentModel.BindingList(Of System.Windows.Forms.TreeNode)
Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides Function FindCore(ByVal prop As System.ComponentModel.PropertyDescriptor,_
ByVal key As Object) As Integer
'
忽略
prop
值,直接遍历列表进行查找匹配
Dim i As Integer
While i < Count
If Items(i).Text.ToLower() = CStr(key).ToLower() Then
Return i
End If
i += 1
End While
Return -1
End Function
End Class
|
也可以在设计时,通过属相窗口定义ListBox控件的DataSource和DisplayMember属性,来实现控件数据绑定。
1.1.2 新增操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Add方法实现向ListBox控件新增数据项。清单4-13演示向ListBox控件新增数据项。
清单 4-13 向ListBox控件新增数据项
……
If BindingSource1.AllowNew <> True Then
Windows.Forms.MessageBox.Show("
不能增加列表数据项。
")
Else
Dim foundIndex As Integer = BindingSource1.Add(New Windows.Forms.TreeNode(TextBox1.Text))
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
增加列表数据项
" + TextBox1.Text + "
失败。
")
End If
End If
……
|
1.1.3 删除操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的RemoveAt方法实现从ListBox控件删除数据项。清单4-14演示从ListBox控件删除数据项。
清单 4-14 删除ListBox数据项
……
If BindingSource1.AllowRemove <> True Then
Windows.Forms.MessageBox.Show("
不能删除列表数据项。
")
Else
BindingSource1.RemoveAt(BindingSource1.Find("Text", TextBox1.Text))
End If
……
|
1.1.4 查找操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Find方法实现从ListBox控件查找数据项。清单4-15演示从ListBox控件查找数据项。
清单 4-15 查找ListBox数据项
……
If BindingSource1.SupportsSearching <> True Then
Windows.Forms.MessageBox.Show("
不能查找列表。
")
Else
Dim foundIndex As Integer = BindingSource1.Find("Text", TextBox1.Text)
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
没有查找到
" + TextBox1.Text)
End If
End If
……
|
BindingSource控件是.NET Compact Framework 2.0提供的新控件之一。BindingSource控件与数据源建立连接,然后将窗体中的控件与BindingSource控件建立绑定关系来实现数据绑定,简化数据绑定的过程。BindingSource控件即是一个连接后台数据库的渠道,同时又是一个数据源,因为BindingSource控件即支持向后台数据库发送命令来检索数据,又支持直接通过BindingSource控件对数据进行访问、排序、筛选和更新操作。BindingSource控件能够自动管理许多绑定问题。BindingSource控件没有运行时界面,无法在用户界面上看到该控件。
BindingSource控件通过Current属性访问当前记录,通过List属性访问整个数据表。表4-8列出了BindingSource控件的主要属性。
表4-8:BindingSource的主要属性
属性
|
说明
|
AllowEdit
|
指示是否可以编辑
BindingSource
控件中的记录。
|
AllowNew
|
指示是否可以使用
AddNew
方法向
BindingSource
控件添加记录。
|
AllowRemove
|
指示是否可从
BindingSource
控件中删除记录。
|
Count
|
获取
BindingSource
控件中的记录数。
|
CurrencyManager
|
获取与
BindingSource
控件关联的当前记录管理器。
|
Current
|
获取
BindingSource
控件中的当前记录。
|
DataMember
|
获取或设置连接器当前绑定到的数据源中的特定数据列表或数据库表。
|
DataSource
|
获取或设置连接器绑定到的数据源。
|
Filter
|
获取或设置用于筛选的表达式。
|
Item
|
获取或设置指定索引的记录。
|
Sort
|
获取或设置用于排序的列名来指定排序。
|
通过Current属性及RemoveCurrent、EndEdit、CancelEdit、Add和AddNew方法可实现对当前记录的编辑操作。表4-3列出了BindingSource控件的主要方法。
表4-3:BindingSource的主要方法
方法
|
说明
|
Add
|
将现有项添加到内部列表中。
|
CancelEdit
|
取消当前编辑操作。
|
Clear
|
从列表中移除所有元素。
|
EndEdit
|
将挂起的更改应用于基础数据源。
|
Find
|
在数据源中查找指定的项。
|
MoveFirst
|
移至列表中的第一项。
|
MoveLast
|
移至列表中的最后一项。
|
MoveNext
|
移至列表中的下一项。
|
MovePrevious
|
移至列表中的上一项。
|
RemoveCurrent
|
从列表中移除当前项。
|
1.1.1 绑定操作
清单4-11演示通过BindingSource控件将包含TreeNode列表的BindingList对象绑定到ListBox控件。清单4-12演示包含TreeNode列表的BindingList对象的类定义。
清单 4-11 使用BindingSource控件进行绑定
……
Dim myNodeList As New MyTreeNodeList()
myNodeList.Add(New Windows.Forms.TreeNode("
西藏
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广西壮族自治区
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
四川省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
河南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
陕西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
湖南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
吉林省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
宁夏回族自治区
"))
BindingSource1.DataSource = myNodeList
ListBox1.DataSource = BindingSource1
ListBox1.DisplayMember = "Text"
……
|
清单 4-12 BindingList对象的类定义
Public Class MyTreeNodeList
Inherits System.ComponentModel.BindingList(Of System.Windows.Forms.TreeNode)
Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides Function FindCore(ByVal prop As System.ComponentModel.PropertyDescriptor,_
ByVal key As Object) As Integer
'
忽略
prop
值,直接遍历列表进行查找匹配
Dim i As Integer
While i < Count
If Items(i).Text.ToLower() = CStr(key).ToLower() Then
Return i
End If
i += 1
End While
Return -1
End Function
End Class
|
也可以在设计时,通过属相窗口定义ListBox控件的DataSource和DisplayMember属性,来实现控件数据绑定。
1.1.2 新增操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Add方法实现向ListBox控件新增数据项。清单4-13演示向ListBox控件新增数据项。
清单 4-13 向ListBox控件新增数据项
……
If BindingSource1.AllowNew <> True Then
Windows.Forms.MessageBox.Show("
不能增加列表数据项。
")
Else
Dim foundIndex As Integer = BindingSource1.Add(New Windows.Forms.TreeNode(TextBox1.Text))
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
增加列表数据项
" + TextBox1.Text + "
失败。
")
End If
End If
……
|
1.1.3 删除操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的RemoveAt方法实现从ListBox控件删除数据项。清单4-14演示从ListBox控件删除数据项。
清单 4-14 删除ListBox数据项
……
If BindingSource1.AllowRemove <> True Then
Windows.Forms.MessageBox.Show("
不能删除列表数据项。
")
Else
BindingSource1.RemoveAt(BindingSource1.Find("Text", TextBox1.Text))
End If
……
|
1.1.4 查找操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Find方法实现从ListBox控件查找数据项。清单4-15演示从ListBox控件查找数据项。
清单 4-15 查找ListBox数据项
……
If BindingSource1.SupportsSearching <> True Then
Windows.Forms.MessageBox.Show("
不能查找列表。
")
Else
Dim foundIndex As Integer = BindingSource1.Find("Text", TextBox1.Text)
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
没有查找到
" + TextBox1.Text)
End If
End If
……
|
BindingSource控件是.NET Compact Framework 2.0提供的新控件之一。BindingSource控件与数据源建立连接,然后将窗体中的控件与BindingSource控件建立绑定关系来实现数据绑定,简化数据绑定的过程。BindingSource控件即是一个连接后台数据库的渠道,同时又是一个数据源,因为BindingSource控件即支持向后台数据库发送命令来检索数据,又支持直接通过BindingSource控件对数据进行访问、排序、筛选和更新操作。BindingSource控件能够自动管理许多绑定问题。BindingSource控件没有运行时界面,无法在用户界面上看到该控件。
BindingSource控件通过Current属性访问当前记录,通过List属性访问整个数据表。表4-8列出了BindingSource控件的主要属性。
表4-8:BindingSource的主要属性
属性
|
说明
|
AllowEdit
|
指示是否可以编辑
BindingSource
控件中的记录。
|
AllowNew
|
指示是否可以使用
AddNew
方法向
BindingSource
控件添加记录。
|
AllowRemove
|
指示是否可从
BindingSource
控件中删除记录。
|
Count
|
获取
BindingSource
控件中的记录数。
|
CurrencyManager
|
获取与
BindingSource
控件关联的当前记录管理器。
|
Current
|
获取
BindingSource
控件中的当前记录。
|
DataMember
|
获取或设置连接器当前绑定到的数据源中的特定数据列表或数据库表。
|
DataSource
|
获取或设置连接器绑定到的数据源。
|
Filter
|
获取或设置用于筛选的表达式。
|
Item
|
获取或设置指定索引的记录。
|
Sort
|
获取或设置用于排序的列名来指定排序。
|
通过Current属性及RemoveCurrent、EndEdit、CancelEdit、Add和AddNew方法可实现对当前记录的编辑操作。表4-3列出了BindingSource控件的主要方法。
表4-3:BindingSource的主要方法
方法
|
说明
|
Add
|
将现有项添加到内部列表中。
|
CancelEdit
|
取消当前编辑操作。
|
Clear
|
从列表中移除所有元素。
|
EndEdit
|
将挂起的更改应用于基础数据源。
|
Find
|
在数据源中查找指定的项。
|
MoveFirst
|
移至列表中的第一项。
|
MoveLast
|
移至列表中的最后一项。
|
MoveNext
|
移至列表中的下一项。
|
MovePrevious
|
移至列表中的上一项。
|
RemoveCurrent
|
从列表中移除当前项。
|
1.1.1 绑定操作
清单4-11演示通过BindingSource控件将包含TreeNode列表的BindingList对象绑定到ListBox控件。清单4-12演示包含TreeNode列表的BindingList对象的类定义。
清单 4-11 使用BindingSource控件进行绑定
……
Dim myNodeList As New MyTreeNodeList()
myNodeList.Add(New Windows.Forms.TreeNode("
西藏
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
广西壮族自治区
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
四川省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山东省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
河南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
陕西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
海南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
湖南省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
吉林省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
山西省
"))
myNodeList.Add(New Windows.Forms.TreeNode("
宁夏回族自治区
"))
BindingSource1.DataSource = myNodeList
ListBox1.DataSource = BindingSource1
ListBox1.DisplayMember = "Text"
……
|
清单 4-12 BindingList对象的类定义
Public Class MyTreeNodeList
Inherits System.ComponentModel.BindingList(Of System.Windows.Forms.TreeNode)
Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides Function FindCore(ByVal prop As System.ComponentModel.PropertyDescriptor,_
ByVal key As Object) As Integer
'
忽略
prop
值,直接遍历列表进行查找匹配
Dim i As Integer
While i < Count
If Items(i).Text.ToLower() = CStr(key).ToLower() Then
Return i
End If
i += 1
End While
Return -1
End Function
End Class
|
也可以在设计时,通过属相窗口定义ListBox控件的DataSource和DisplayMember属性,来实现控件数据绑定。
1.1.2 新增操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Add方法实现向ListBox控件新增数据项。清单4-13演示向ListBox控件新增数据项。
清单 4-13 向ListBox控件新增数据项
……
If BindingSource1.AllowNew <> True Then
Windows.Forms.MessageBox.Show("
不能增加列表数据项。
")
Else
Dim foundIndex As Integer = BindingSource1.Add(New Windows.Forms.TreeNode(TextBox1.Text))
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
增加列表数据项
" + TextBox1.Text + "
失败。
")
End If
End If
……
|
1.1.3 删除操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的RemoveAt方法实现从ListBox控件删除数据项。清单4-14演示从ListBox控件删除数据项。
清单 4-14 删除ListBox数据项
……
If BindingSource1.AllowRemove <> True Then
Windows.Forms.MessageBox.Show("
不能删除列表数据项。
")
Else
BindingSource1.RemoveAt(BindingSource1.Find("Text", TextBox1.Text))
End If
……
|
1.1.4 查找操作
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Find方法实现从ListBox控件查找数据项。清单4-15演示从ListBox控件查找数据项。
清单 4-15 查找ListBox数据项
……
If BindingSource1.SupportsSearching <> True Then
Windows.Forms.MessageBox.Show("
不能查找列表。
")
Else
Dim foundIndex As Integer = BindingSource1.Find("Text", TextBox1.Text)
If foundIndex > -1 Then
ListBox1.SelectedIndex = foundIndex
Else
Windows.Forms.MessageBox.Show("
没有查找到
" + TextBox1.Text)
End If
End If
……
|