BindingSource 组件.doc_第1页
BindingSource 组件.doc_第2页
BindingSource 组件.doc_第3页
BindingSource 组件.doc_第4页
BindingSource 组件.doc_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

BindingSource 组件.NET Framework 4.5其他版本此主题尚未评级-评价此主题封装数据源以绑定到控件。BindingSource组件有两个用途。首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将BindingSource组件绑定到数据源,然后将窗体上的控件绑定到BindingSource组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用BindingSource组件来完成的。其次,BindingSource组件可以充当强类型数据源。使用Add方法向BindingSource组件添加类型会创建一个该类型的列表。本节内容BindingSource 组件概述介绍BindingSource组件的一般概念,该组件用于将数据源绑定到控件。如何:将 Windows 窗体控件绑定到 DBNull 数据库值演示如何使用BindingSource组件处理来自数据源的DBNull值。如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选演示如何使用BindingSource组件向显示的数据应用排序和筛选器。如何:使用 Windows 窗体 BindingSource 绑定到 Web 服务演示如何使用BindingSource组件绑定到 Web 服务。如何:处理因数据绑定而发生的错误和异常演示如何使用BindingSource组件正确处理数据绑定操作中发生的错误。如何:将 Windows 窗体控件绑定到类型演示如何使用BindingSource组件绑定到类型。如何:将 Windows 窗体控件绑定到 Factory 对象演示如何使用BindingSource组件绑定到工厂对象或方法。如何:使用 Windows 窗体 BindingSource 自定义项添加演示如何使用BindingSource组件创建新的项并将这些项添加到数据源。如何:使用 BindingSource ResetItem 方法引发更改通知演示如何使用BindingSource组件为不支持更改通知的数据源引发更改通知事件。如何:使用 BindingSource 和 INotifyPropertyChanged 接口引发更改通知演示如何将从INotifyPropertyChanged继承的类型与BindingSource控件一起使用。如何:使用 BindingSource 在 Windows 窗体控件中反映数据源更新演示如何使用BindingSource组件响应数据源中的更改。如何:使用 BindingSource 组件跨窗体共享绑定数据演示如何使用BindingSource将多个窗体绑定到同一数据源。TopicLocation如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定Windows 窗体控件如何:使用 Windows 窗体 BindingSource 组件创建查找表Windows 窗体控件如何:使用设计器将 Windows 窗体控件绑定到类型Windows 窗体控件如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定Windows 窗体控件如何:使用设计器将 Windows 窗体控件绑定到类型Windows 窗体控件如何:使用 Windows 窗体 BindingSource 组件创建查找表Windows 窗体控件如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定Windows 窗体控件如何:使用设计器将 Windows 窗体控件绑定到类型Windows 窗体控件如何:使用 Windows 窗体 BindingSource 组件创建查找表Windows 窗体控件如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定dv_mclictl如何:使用设计器将 Windows 窗体控件绑定到类型dv_mclictl如何:使用 Windows 窗体 BindingSource 组件创建查找表dv_mclictl如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定dv_mclictl如何:使用设计器将 Windows 窗体控件绑定到类型dv_mclictl如何:使用 Windows 窗体 BindingSource 组件创建查找表如何:使用设计器将 Windows 窗体控件与 BindingSource 组件进行绑定.NET Framework 4.5其他版本此主题尚未评级-评价此主题将控件添加到窗体并确定应用程序的用户界面后,可以将控件绑定到数据源,这样用户就可以在运行时改变和保存与应用程序相关的数据。将BindingSource控件用作窗体上的控件和数据源之间的桥梁,能够最轻松地完成 Windows 窗体中一个控件或一系列控件的绑定。可以将窗体上的一个或多个控件绑定到数据;在下面的过程中,将一个TextBox控件绑定到一个数据源。为了完成此过程,假定将绑定到派生自数据库的数据源。有关从其他数据存储区创建数据源的更多信息,请参见数据源概述。说明显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见Visual Studio 设置。在设计时绑定控件1. 将TextBox控件拖到窗体上。2. 在“属性”窗口中:a. 展开“(DataBindings)”节点。b. 单击Text属性旁边的箭头。此时将打开“DataSource”UI 类型编辑器。如果以前已经为项目或窗体配置了数据源,将显示此数据源。3. 单击“添加项目数据源”以连接到数据并创建一个数据源。4. 在“数据源配置向导”欢迎页上单击“下一步”。5. 在“选择数据源类型”页上选择“数据库”。6. 在“选择您的数据连接”页上,从可用连接列表中选择一个数据连接。如果所需的数据连接不可用,请选择“新建连接”以创建新的数据连接。7. 选择“是,保存连接”,以保存应用程序配置文件中的连接字符串。8. 选择要放置到应用程序中的数据库对象。在此例中,在表中选择一个希望显示TextBox的字段。9. 如果愿意,可以替换默认的数据集名称。10. 单击“完成”。11. 在“属性”窗口中,再次单击Text属性旁的箭头。在“DataSource”UI 类型编辑器中,选择要将TextBox绑定到的字段的名称。“DataSource”UI 类型编辑器关闭,并且特定于该数据连接的数据集、BindingSource和表适配器将添加到窗体中。如何:将 Windows 窗体控件绑定到 DBNull 数据库值.NET Framework 4.5其他版本此主题尚未评级-评价此主题将 Windows 窗体控件绑定到数据源,而数据源返回DBNull值时,不经过处理、格式化或分析事件就可以替代相应值。格式化或分析数据源值时,NullValue属性将DBNull转换为指定对象。示例下面的示例演示如何在两种不同情况下绑定DBNull值。第一种情况演示如何设置字符串属性的NullValue;第二种情况演示如何设置图像属性的NullValue。C#VBImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.TextImports System.Data.SqlClientImports System.Windows.FormsPublic Class Form1 Inherits Form Public Sub New() End Sub The controls and components we need for the form. Private WithEvents button1 As Button Private pictureBox1 As PictureBox Private bindingSource1 As BindingSource Private textBox1 As TextBox Private textBox2 As TextBox Data table to hold the database data. Private employeeTable As New DataTable() Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _ Handles Me.Load Basic form setup. Me.pictureBox1 = New PictureBox() Me.bindingSource1 = New BindingSource() Me.textBox1 = New TextBox() Me.textBox2 = New TextBox() Me.button1 = New Button() Me.pictureBox1.Location = New System.Drawing.Point(20, 20) Me.pictureBox1.Size = New System.Drawing.Size(174, 179) Me.textBox1.Location = New System.Drawing.Point(25, 215) Me.textBox1.ReadOnly = True Me.textBox2.Location = New System.Drawing.Point(25, 241) Me.textBox2.ReadOnly = True Me.button1.Location = New System.Drawing.Point(200, 103) Me.button1.Text = Move Next Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.button1) Me.Controls.Add(Me.textBox2) Me.Controls.Add(Me.textBox1) Me.Controls.Add(Me.pictureBox1) Me.ResumeLayout(False) Me.PerformLayout() Create the connection string and populate the data table with data. Dim connectionString As String = Integrated Security=SSPI; & _ Persist Security Info = False;Initial Catalog=Northwind; _ & Data Source = localhost Dim connection As New SqlConnection() connection.ConnectionString = connectionString Dim employeeAdapter As New SqlDataAdapter _ (New SqlCommand(Select * from Employees, connection) connection.Open() employeeAdapter.Fill(employeeTable) Set the DataSource property of the BindingSource to the employee table. bindingSource1.DataSource = employeeTable Set up the binding to the ReportsTo column. Dim reportsToBinding As Binding = _ textBox2.DataBindings.Add(Text, bindingSource1, ReportsTo, _ True) Set the NullValue property for this binding. reportsToBinding.NullValue = No Manager Set up the binding for the PictureBox using the Add method, setting the null value in method call. pictureBox1.DataBindings.Add(Image, bindingSource1, Photo, _ True, DataSourceUpdateMode.Never, _ New Bitmap(GetType(Button), Button.bmp) Set up the remaining binding. textBox1.DataBindings.Add(Text, bindingSource1, LastName, True) End Sub Move through the data when the button is clicked. Private Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click bindingSource1.MoveNext() End Sub _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1() End SubEnd Class绑定的属性和NullValue属性的类型必须相同,否则将发生错误,而且不会再处理NullValue值。在这种情况下,不会引发异常。如何:使用 Windows 窗体 BindingSource 组件创建查找表.NET Framework 4.5其他版本此主题尚未评级-评价此主题查找表是一种数据表,其中有一列显示另一个相关表的记录数据。在下面的过程中,使用了一个ComboBox控件来显示具有外键关系父、子表的字段。为了帮助实现这两个表和这种关系的可视化,下面是一个父、子表的示例:CustomersTable(父表)CustomerIDCustomerName712Paul Koch713Tamara JohnstonOrdersTable(子表)OrderIDOrderDateCustomerID9032004 年 2 月 12 日7129042004 年 2 月 13 日713在这一情形下,表 CustomersTable 存储了需要显示和保存的实际信息。但为了节省空间,此表省略了大部分数据。另一张表 OrdersTable 只包含有关哪个客户 ID 号等同于哪个订单日期和订单 ID 的表面相关信息,并没有提及客户的名称。在ComboBox 控件(Windows 窗体)控件上设置了 4 种重要属性来创建查找表。 DataSource属性包含查找表的名称。 DisplayMember属性包含查找表中要作为控件文本(客户名称)显示的数据列。 ValueMember属性包含查找表中具有存储信息(父表中的 ID 号)的数据列。 SelectedValue属性根据ValueMember为子表提供查找值。下面的过程演示了如何将窗体布局成为一个查找表,并将数据绑定到它上面的控件。为了成功完成这个过程,必须像上面提到的一样有一个带有存在外键关系的父表和子表的数据源。创建用户界面1. 从“工具箱”中将一个ComboBox控件中拖动到窗体上。此控件将显示父表的列。2. 拖动其他的控件来显示子表的详细信息。表中数据的格式决定了应当选择哪一种控件。有关更多信息,请参见根据功能列出的 Windows 窗体控件。3. 将一个BindingNavigator控件拖到窗体上,这将允许您定位子表中的数据。连接数据并将其绑定到控件1. 选择ComboBox并单击“智能任务”标志符号以显示“智能任务”对话框。2. 选择“使用数据绑定项”。3. 单击“数据源”下拉框旁边的箭头。如果以前已经为项目或窗体配置了数据源,将显示该数据源;否则,完成下面的步骤(此示例使用 Northwind 示例数据库中的 Customers 表和 Orders 表,并在括号中引用它们)。a. 单击“添加项目数据源”以连接到数据并创建一个数据源。b. 在“数据源配置向导”欢迎页上单击“下一步”。c. 在“选择数据源类型”页面上选择“数据库”。d. 从“选择数据连接”页上的可用连接列表中选择一个数据连接。如果希望的数据连接不可用,则选择“新连接”以创建一个新的数据连接。有关更多信息,请参见“添加/修改连接”对话框(通用)。e. 单击“是,保存连接”将连接字符串保存到应用程序配置文件中。f. 选择要放置到应用程序中的数据库对象。在这种情况下,选择具有外键关系的一个父表和一个子表(例如 Customers 和 Orders)。g. 如果愿意,可以替换默认的数据集名称。h. 单击“完成”。4. 在“显示成员”下拉框中,选择将在组合框中显示的列名(例如,ContactName)。5. 在“值成员”下拉框中,选择在子表中进行查找操作的列(例如,CustomerID)。6. 在“选定值”下拉框中,定位到“项目数据源”和刚创建的包含父表和子表的数据集。选择与父表的值成员相同的子表属性(例如,Orders.CustomerID)。将创建适当的BindingSource、数据集和表适配器组件,并添加到窗体中。7. 将BindingNavigator控件绑定到子表的BindingSource(例如,OrdersBindingSource)。8. 从想要显示的子表的BindingSource(例如,OrdersBindingSource)中,将除了ComboBox和BindingNavigator外的控件绑定到详细信息字段上。如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选.NET Framework 4.5其他版本此主题尚未评级-评价此主题您可以通过Sort和Filter属性公开BindingSource控件的排序和筛选功能。当基础数据源为IBindingList时可以应用简单排序,当数据源为IBindingListView时可以应用筛选和高级排序。Sort属性需要标准 ADO.NET 语法:在表示数据源中数据列名的字符串之后加上ASC或DESC,以指示该列表应按升序排序,还是应按降序排序。通过用逗号分隔符隔开每一列,可以设置高级排序或多列排序。Filter属性获取字符串表达式。说明将敏感信息(如密码)存储在连接字符串中可能会影响您的应用程序的安全性。若要控制对数据库的访问,一种较为安全的方法是使用 Windows 身份验证(也称为集成安全性)。有关更多信息,请参见保护连接信息。使用 BindingSource 筛选数据 将Filter属性设置为所需的表达式。在下面的代码示例中,表达式就是在列名之后加上该列所需的值。C#VBBindingSource1.Filter = ContactTitle=Owner使用 BindingSource 进行数据排序1. 将Sort属性设置为后面要跟有ASC或DESC的所需列名称,以指示是按升序还是降序排序。2. 用逗号分隔多个列。C#VBBindingSource1.Sort = Country DESC, Address ASC示例下面的代码示例将 Northwind 示例数据库的 Customers 表中的数据加载到DataGridView控件中,然后对显示的数据进行筛选和排序。C#VBPrivate Sub InitializeSortedFilteredBindingSource() Create the connection string, data adapter and data table. Dim connectionString As New SqlConnection(Initial Catalog=Northwind; & _ Data Source=localhost;Integrated Security=SSPI;) Dim customersTableAdapter As New SqlDataAdapter(Select * from Customers, _ connectionString) Dim customerTable As New DataTable() Fill the the adapter with the contents of the customer table. customersTableAdapter.Fill(customerTable) Set data source for BindingSource1. BindingSource1.DataSource = customerTable Filter the items to show contacts who are owners. BindingSource1.Filter = ContactTitle=Owner Sort the items on the company name in descending order. BindingSource1.Sort = Country DESC, Address ASC Set the data source for dataGridView1 to BindingSource1. dataGridView1.DataSource = BindingSource1End Sub如何:使用 Windows 窗体 BindingSource 绑定到 Web 服务.NET Framework 4.5其他版本此主题尚未评级-评价此主题如果希望将 Windows 窗体控件绑定到通过调用 XML Web services 获取的结果,可以使用BindingSource组件。此过程类似于将BindingSource组件绑定到类型。您必须创建一个客户端代理,其中包含由 Web 服务公开的方法和类型。客户端代理可以由 Web 服务 (.asmx) 本身或其 Web 服务描述语言 (WSDL) 文件生成。另外,客户端代理必须将 Web 服务使用的复杂类型的字段作为公共属性公开。然后将BindingSource绑定到在 Web 服务代理中公开的一种类型。创建和绑定到客户端代理1. 在所选择的目录中以适当的命名空间创建 Windows 窗体。2. 将一个BindingSource组件添加到窗体中。3. 打开 Windows 软件开发包 (SDK) 命令提示符,并定位到窗体所在的目录。4. 使用 WSDL 工具为 Web 服务的 .asmx 或 WSDL 文件输入wsdl和 URL,然后输入应用程序的命名空间,也可以选择输入所使用的语言。下面的代码示例使用位于 /zipcoderesolver/zipcoderesolver.asmx 位置的 Web 服务。例如,适用于 C# 类型的wsdl .zipcoderesolver/zipcoderesolver.asmx /n:BindToWebService,或适用于 Visual Basic 类型的wsdl .zipcoderesolver/zipcoderesolver.asmx /n:BindToWebService /language:VB。将路径作为参数传递给 WSDL 工具,将使用指定的语言在应用程序所在的目录和命名空间中生成客户端代理。如果您使用的是 Visual Studio,则将文件添加到项目中。5. 在客户端代理中选择要绑定到的类型。它通常是由 Web 服务提供的方法返回的类型。为了进行绑定,所选择类型的字段必须公开为公共属性。C#C+VB _Public Class USPSAddress Private streetField As String Private cityField As String Private stateField As String Private shortZIPField As String Private fullZIPField As String Public Property Street() As String Get Return Me.streetField End Get Set(ByVal value As String) Me.streetField = value End Set End Property Public Property City() As String Get Return Me.cityField End Get Set(ByVal value As String) Me.cityField = value End Set End Property Public Property State() As String Get Return Me.stateField End Get Set(ByVal value As String) Me.stateField = value End Set End Property Public Property ShortZIP() As String Get Return Me.shortZIPField End Get Set(ByVal value As String) Me.shortZIPField = value End Set End Property Public Property FullZIP() As String Get Return Me.fullZIPField End Get Set(ByVal value As String) Me.fullZIPField = value End Set End PropertyEnd Class6. 将BindingSource的DataSource属性设置为 Web 服务客户端代理中包含的所需类型。C#C+VBBindingSource1.DataSource = GetType(USPSAddress)将控件绑定到 BindingSource(已绑定到 Web 服务) 通过将所需的 Web 服务类型的公共属性作为参数进行传递,将控件绑定到BindingSource。C#C+VBtextBox1.DataBindings.Add(Text, Me.BindingSource1, FullZIP, True)示例下面的代码示例演示如何将一个BindingSource组件绑定到 Web 服务,然后将一个文本框绑定到BindingSource组件。当您单击该按钮时,将调用一个 Web 服务方法,结果显示在textbox1中。C#C+VBImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DrawingImports System.Windows.FormsNamespace BindToWebService Class Form1 Inherits Form _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1() End Sub Private BindingSource1 As New BindingSource() Private textBox1 As New TextBox() Private textBox2 As New TextBox() Private WithEvents button1 As New Button() Public Sub New() textBox1.Location = New System.Drawing.Point(118, 131) textBox1.ReadOnly = True button1.Location = New System.Drawing.Point(133, 60) button1.Text = Get zipcode ClientSize = New System.Drawing.Size(292, 266) Controls.Add(Me.button1) Controls.Add(Me.textBox1) End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles button1.Click textBox1.Text = Calling Web service. Dim resolver As New ZipCodeResolver() BindingSource1.Add(resolver.CorrectedAddressXml(0, One Microsoft Way, Redmond, WA) End Sub Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load BindingSource1.DataSource = GetType(USPSAddress) textBox1.DataBindings.Add(Text, Me.BindingSource1, FullZIP, True) End Sub End Class _ Public Class ZipCodeResolver Inherits System.Web.Services.Protocols.SoapHttpClientProtocol Private CorrectedAddressXmlOperationCompleted As _ System.Threading.SendOrPostCallback Public Sub New() MyBase.New() Me.Url = _ /zipcoderesolver/zipcoderesolver.asmx End Sub Public Event CorrectedAddressXmlCompleted As _ CorrectedAddressXmlCompletedEventHandler _ Public Function CorrectedAddressXml(ByVal accessCode As String, _ ByVal address As String, ByVal city As String, ByVal state As String) _ As USPSAddress Dim results() As Object = Me.Invoke(CorrectedAddressXml, _ New Object() accessCode, address, city, state) Return CType(results(0), USPSAddress) End Function Public Function BeginCorrectedAddressXml(ByVal accessCode As String, _ ByVal address As String, ByVal city As String, ByVal state As String, _ ByVal callback As System.AsyncCallback, ByVal asyncState As Object) _ As System.IAsyncResult Return Me.BeginInvoke(CorrectedAddressXml, _ New Object() accessCode, address, city, state, callback, asyncState) End Function Public Function EndCorrectedAddressXml(ByVal asyncResult _ As System.IAsyncResult) As USPSAddress Dim results() As Object = Me.EndInvoke(asyncResult) Re

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论