BindingNavigator控件Windows窗体_第1页
BindingNavigator控件Windows窗体_第2页
BindingNavigator控件Windows窗体_第3页
BindingNavigator控件Windows窗体_第4页
BindingNavigator控件Windows窗体_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

1、Bi ndin gNavigator 控件(Windows 窗体).NET Framework 4.5其他版本此主题尚未评级-评价此主题Bindin gNavigator 控件是绑定到数据的控件的导航和操作用户界面(UI)。使用BindingNavigator 控件,用户可以在 Windows窗体中导航和操作数据。本节中的主题对 Bindin gNavigator 控件进行了简要介绍,并提供有关如何使用该控件导航数 据和在DataSet中移动的分步说明。本节内容BindingNavigator 控件概述(Windows 窗体)介绍Bin di ngNavigator 控件的一般概念,使用该控件

2、,用户可以在数据源的项中进行 移动。如何:使用 Windows 窗体BindingNavigator 控件定位数据 提供将Bindin gNavigator 控件绑定到数据源的步骤。如何:使用 Windows 窗体BindingNavigator控件浏览数据集演示如何使用BindingNavigator控件在DataSet的记录中移动TopicLocati on如何:向 Windows窗体BindingNavigator控件添加“加载”、“保存”和“取消”按钮Win dows 窗体 控件如何:向 Windows窗体BindingNavigator控件添加“加载”、“保存”和“取消”按钮Win

3、dows 窗体 控件如何:向 Windows窗体BindingNavigator控件添加“加载”、“保存”和“取消”按钮Win dows 窗体 控件如何:向 Windows窗体BindingNavigator控件添加“加载”、“保存”和“取消”按钮dv_mclictlTopicLocati on如何:向 Windows窗体BindingNavigator控件添加“加载”、“保存”和“取消”按钮dv_mclictl参考Bindin gNavigator提供BindingNavigator控件的参考文档Bindin gSource提供 BindingSource控件的参考文档。相关章节TopicL

4、ocati on在Win dows应用程序中的窗体上显示数 据Visual Studio中的数据访问在Win dows应用程序中的窗体上显示数 据Visual Studio中的数据访问在Visual Studio中将控件绑定到数据dv_raddata在Visual Studio中将控件绑定到数据dv_raddata在Visual Studio中将控件绑定到数据Bin di ngNavigator控件概述(Windows 窗体).NET Framework 4.5其他版本此主题尚未评级-评价此主题可使用Bin di ngNavigator 控件创建标准化方法,以供用户搜索和更改Win dows窗

5、体中的数据。通常将Bin di ngNavigator 与Bin di ngSource 组件一起使用,这样用户可以在窗体的数据 记录之间移动并与这些记录进行交互。Bi ndi ngNavigator 的工作方式Bindin gNavigator控件由ToolStrip 和一系列 ToolStripItem对象组成,完成大多数常见的与数据相关的操作:添加数据、删除数据和定位数据。默认情况下,Bindin gNavigator 控件包含这些标准按钮。下面的屏幕快照显示窗体中的Bindin gNavigator 控件。:H 1 | 2 of 3 I H I p下表列出这些控件并描述其功能。控件功能

6、AddNewItem 按钮将新行插入到基础数据源。DeleteItem 按钮从基础数据源删除当前行。MoveFirstItem 按钮移动到基础数据源的第一项。MoveLastItem 按钮移动到基础数据源的最后一项。MoveNextItem 按钮移动到基础数据源的下一项。MovePreviousItem 按钮移动到基础数据源的上一项。Positio nltem 文本框返回基础数据源内的当前位置。Countltem文本框返回基础数据源内总的项数。对于此集合中的每个控件,Bin di ngSource组件中都有一个对应的成员,这些成员以编程方式提供相同功能。 例如,MoveFirstltem按钮对

7、应于 Bin di ngSource组件的MoveFirst 方法,DeleteItem按钮对应于 RemoveCurrent方法,等等。如果默认按钮不适合您的应用程序,或者如果您需要其他按钮来支持其他类型的功能,则可以提供您自己的 ToolStrip 按钮。如何:使用Windows窗体 BindingNavigator控件定位数据.NET Framework 4.5其他版本此主题尚未评级-评价此主题Win dows窗体提供Bin di ngNavigator控件,开发人员可通过该控件在他们创建的窗体上,为最终用户提供简单的数据导航和用户界面操作。Bin di ngNavigator控件是一个

8、 ToolStrip控件,该控件上带有预配置为定位到数据集中第一条、最后一条、下一条和上一条记录的按钮,而且还有用于添加和删除记录的按钮。将按钮添加到BindingNavigator控件比较容易,因为它是一个ToolStrip 控件。对于 Bin di ngNavigator控件上的每个按钮,都有一个对应的Bin di ngSource 组件成员,其以编程方式允许有相同功能。例如,MoveFirstltem按钮对应于 Bindin gSource 组件的 MoveFirst 方法,DeleteItem 按钮对应于 RemoveCurrent 方法,等等。 这样,启 用BindingNaviga

9、tor控件定位数据记录就如同在窗体上将其BindingSource属性设置为适当的Bin di ngSource 组件一样简单。设置 BindingNavigator控件1. 添加一个名为 bindingSource1的BindingSource 组件和两个名为 textBox1 和 textBox2 的 TextBox 控件。2. 将bindingSource1 绑定到数据,将 TextBox控件绑定到 bindingSource1 。若要执行此操作,请将下面的代码粘贴到窗体中,并从窗体的构造函数调用LoadData 或调用Load事件处理方法。C#VBPrivate SubLoadData

10、()'The xml to bind to.Dim xml As String = "<US><states>" +"<state> <n ame>Wash ington</n ame><capital>Olympia</capital>v/state>" +"<state> <n ame>Oreg on</n ame><capital>Salem</capital>v/state>

11、"+"<state> <n ame>Califor nia</n ame><capital>Sacrame nto</capital>v/s tate>" + "<state> <n ame>Nevada</name><capital>Cars onCity</c apitalx/state>"+ "</states></US>"'Con vert the xml st

12、ri ng to bytes and load in to a memorystream. Dim xmlBytes As Byte () = Encoding.UTF8.GetBytes(xml) Dim stream As NewMemoryStream(xmlBytes, False)'Create a DataSet and load the xml into it.Dim set As NewDataSet()set .ReadXml(stream)Set the DataSource to the DataSet, and the DataMember to state.b

13、indin gSource1.DataSource = set bi ndi ngSource1.DataMember = "state"textBox1.DataBi ndi ngs.Add(textBox2.DataBi ndi ngs.Add("Text" , bin di ngSource1,"n ame")"Text" , bindingSource1, "capital")End Sub 'LoadData3. 将名为 bindingNavigator1的 BindingNa

14、vigator控件添加到窗体。4. 将 bindingNavigator1的 BindingSource 属性设置为 bindingSource1。可以使用设计器或用代码执行此操作。C#VBbindin gNavigator1.B indin gSource = bindin gSource1示例下面的代码示例是前面所列步骤的完整示例。C#VBImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports S

15、ystem.Windows.FormsImports System.XmlImports System.IOImports System.TextClass Form1Inherits FormPrivate components As IContainerPrivate bindingNavigator1 As BindingNavigatorAs ToolStripButtonAs ToolStripLabelAs ToolStripButtonPrivate bindingNavigatorAddNewItemPrivate bindingNavigatorCountItemPrivat

16、e bindingNavigatorDeleteItemPrivate bindingNavigatorMoveFirstItem As ToolStripButtonPrivate bindingNavigatorMovePreviousItem As ToolStripButton Private bindingNavigatorSeparator As ToolStripSeparator Private bindingNavigatorPositionItemAs ToolStripTextBoxPrivate bindingNavigatorSeparator1As ToolStri

17、pSeparatorPrivate bindingNavigatorMoveNextItem As ToolStripButton Private bindingNavigatorMoveLastItem As ToolStripButton Private textBox1 As TextBoxPrivate textBox2 As TextBoxPrivate bindingSource1 As BindingSourcePrivate bindingNavigatorSeparator2As ToolStripSeparatorPublic Sub New()InitializeComp

18、onent()LoadData()bindingNavigator1.BindingSource = bindingSource1End Sub 'NewPrivate Sub LoadData()' The xml to bind to.Dim xml As String = "<US><states>" + "<state><name>Washington</name><capital>Olympia</capital></state>"

19、+ "<state><name>Oregon</name><capital>Salem</capital></state>" + "<state><name>California</name><capital>Sacramento</capital></state>" + "<state><name>Nevada</name><capital>Carson

20、 City</capital></state>" "</states></US>"' Convert the xml string to bytes and load into a memory stream. Dim xmlBytes As Byte () = Encoding.UTF8.GetBytes(xml) Dim stream As NewMemoryStream(xmlBytes, False ) ' Create a DataSet and load the xml into it.D

21、im set As NewDataSet() set .ReadXml(stream)Set the DataSource to the DataSet, and the DataMember to state.bindingSource1.DataSource = set bindingSource1.DataMember ="state"textBox1.DataBindings.Add(textBox2.DataBindings.Add("Text" , bindingSource1, "name")"Text&quo

22、t; , bindingSource1, "capital" )End Sub 'LoadData<STAThread()> _Public Shared Sub Main()Application.EnableVisualStyles()Application.Run( NewForm1()End Sub 'MainPrivate Sub InitializeComponent()Mponents = NewSystem.ComponentModel.Container()Dim resources As NewSystem.Component

23、Model.ComponentResourceManager(GetType(Form1)Me.bindingNavigator1 = NewMponents)System.Windows.Forms.BindingNavigator(Me.bindingNavigatorAddNewItem = NewSystem.Windows.Forms.ToolStripButton()Me.bindingNavigatorCountItem = NewSystem.Windows.Forms.ToolStripLabel()Me.bindingNavigatorDeleteItem = NewSys

24、tem.Windows.Forms.ToolStripButton()Me.bindingNavigatorMoveFirstItem = NewSystem.Windows.Forms.ToolStripButton()Me.bindingNavigatorMovePreviousItem = NewSystem.Windows.Forms.ToolStripButton()Me.bindingNavigatorSeparator = NewSystem.Windows.Forms.ToolStripSeparator()Me.bindingNavigatorPositionItem = N

25、ewSystem.Windows.Forms.ToolStripTextBox()Me.bindingNavigatorSeparator1 = NewSystem.Windows.Forms.ToolStripSeparator()Me.bindingNavigatorMoveNextItem = NewSystem.Windows.Forms.ToolStripButton()Me.bindingNavigatorMoveLastItem =NewSystem.Windows.Forms.ToolStripButton()Me.bindingNavigatorSeparator2 = Ne

26、wSystem.Windows.Forms.ToolStripSeparator()Me.textBox1 = NewSystem.Windows.Forms.TextBox()Me.textBox2 = NewSystem.Windows.Forms.TextBox()Me.bindingSource1 = NewSystem.Windows.Forms.BindingSource( Mponents)CType( Me.bindingNavigator1,System.ComponentModel.ISupportInitialize).BeginInit()Me.bindingNavig

27、ator1.SuspendLayout()CType( Me.bindingSource1,System.ComponentModel.ISupportInitialize).BeginInit()Me.SuspendLayout()I' bindingNavigator1Me.bindingNavigator1.AddNewItem =Me.bindingNavigatorAddNewItemMe.bindingNavigator1.CountItem = Me.bindingNavigatorCountItemMe.bindingNavigator1.CountItemFormat

28、 ="of 0"Me.bindingNavigator1.DeleteItem =Me.bindingNavigatorDeleteItemMe.bindingNavigator1.Items.AddRange( NewSystem.Windows.Forms.ToolStripItem() Me.bindingNavigatorMoveFirstItem,Me.bindingNavigatorMovePreviousItem, Me.bindingNavigatorSeparatorMe.bindingNavigatorPositionItem,Me.bindingNav

29、igatorSeparator1,Me.bindingNavigatorMoveLastItem,Me.bindingNavigatorAddNewItem,Me.bindingNavigatorCountItem,Me.bindingNavigatorMoveNextItem,Me.bindingNavigatorSeparator2,Me.bindingNavigatorDeleteItem)Me.bindingNavigator1.Location =NewSystem.Drawing.Point(0, 0)Me.bindingNavigator1.MoveFirstItem = Me.

30、bindingNavigatorMoveFirstItemMe.bindingNavigator1.MoveLastItem =Me.bindingNavigatorMoveLastItemMe.bindingNavigator1.MoveNextItem =Me.bindingNavigatorMoveNextItemMe.bindingNavigator1.MovePreviousItem =Me.bindingNavigatorMovePreviousItemMe.bindingNavigator1.Name = "bindingNavigator1"Me.bindi

31、ngNavigator1.PositionItem =Me.bindingNavigatorPositionItemMe.bindingNavigator1.TabIndex = 2Me.bindingNavigator1.Text ="bindingNavigator1"I' bindingNavigatorAddNewItemIMe.bindingNavigatorAddNewItem.Image = CType(resources.GetObject( "bindingNavigatorAddNewItem.Image" ), System

32、.Drawing.Image)Me.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem"Me.bindingNavigatorAddNewItem.Text = "Add new"I' bindingNavigatorCountItemIMe.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem"Me.bindingNavigatorCountItem.Text = "of 0&q

33、uot;Me.bindingNavigatorCountItem.ToolTipText ="Total number ofitems"' bindingNavigatorDeleteItemIMe.bindingNavigatorDeleteItem.Image =CType(resources.GetObject( "bindingNavigatorDeleteItem.Image" ), System.Drawing.Image)Me.bindingNavigatorDeleteItem.Name = "bindingNaviga

34、torDeleteItem"Me.bindingNavigatorDeleteItem.Text = "Delete"I' bindingNavigatorMoveFirstItemIMe.bindingNavigatorMoveFirstItem.Image =CType(resources.GetObject( "bindingNavigatorMoveFirstItem.Image" ), System.Drawing.Image)Me.bindingNavigatorMoveFirstItem.Name = "bind

35、ingNavigatorMoveFirstItem"Me.bindingNavigatorMoveFirstItem.Text ="Move first"' bindingNavigatorMovePreviousItemIMe.bindingNavigatorMovePreviousItem.Image = CType(resources.GetObject( "bindingNavigatorMovePreviousItem.Image" ), System.Drawing.Image)Me.bindingNavigatorMove

36、PreviousItem.Name = "bindingNavigatorMovePreviousItem"Me.bindingNavigatorMovePreviousItem.Text ="Move previous"I' bindingNavigatorSeparatorIMe.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator"I' bindingNavigatorPositionItemIMe.bindingNavigatorPosition

37、Item.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndTextMe.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem"Me.bindingNavigatorPositionItem.Size =NewSystem.Drawing.Size(50, 25)Me.bindingNavigatorPositionItem.Text ="0"Me.bindingNavigatorPosi

38、tionItem.ToolTipText = "Current position"I' bindingNavigatorSeparator1IMe.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator1"I' bindingNavigatorMoveNextItemIMe.bindingNavigatorMoveNextItem.Image = CType(resources.GetObject( "bindingNavigatorMoveNextItem.Im

39、age" ), System.Drawing.Image)Me.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem"Me.bindingNavigatorMoveNextItem.Text = "Move next"I' bindingNavigatorMoveLastItemMe.bindingNavigatorMoveLastItem.Image = CType(resources.GetObject( "bindingNavigatorMov

40、eLastItem.Image" ), System.Drawing.Image)Me.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem"Me.bindingNavigatorMoveLastItem.Text = "Move last"I' bindingNavigatorSeparator2IMe.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator2"I'

41、 textBox1IMe.textBox1.Location = NewSystem.Drawing.Point(46, 64)Me.textBox1.Name = "textBox1"Me.textBox1.TabIndex = 3I' textBox2IMe.textBox2.Location = NewSystem.Drawing.Point(46, 104)Me.textBox2.Name = "textBox2"Me.textBox2.TabIndex = 4I' Form1IMe.ClientSize = NewSystem.

42、Drawing.Size(292, 266)Me.Controls.Add(textBox2)Me.Controls.Add(textBox1)Me.Controls.Add(bindingNavigator1)Me.Name = "Form1"CType( Me.bindingNavigator1,System.ComponentModel.ISupportInitialize).EndInit()Me.bindingNavigator1.ResumeLayout( False )Me.bindingNavigator1.PerformLayout()CType( Me.

43、bindingSource1,System.ComponentModel.ISupportInitialize).EndInit()Me.ResumeLayout( False )Me.PerformLayout()End Sub 'InitializeComponentEnd Class 'Form1如何:使用Windows窗体Bindin gNavigator数据集控件浏览 NET Framework 4.5其他版本cc此主题尚未评级-评价此主题生成数据驱动的应用程序时,经常需要向用户显示数据集合。Bindin gNavigator控件与Bin di ngSource组件一

44、起为滚动集合并按顺序显示其中的项提供方便的可扩展解决方案。示例下面的代码示例演示如何使用BindingNavigator控件来浏览数据。集合包含在 DataView 中,后者使用 BindingSource组件绑定到 TextBox控件。说明若要控制对数据库将敏感信息(如密码)存储在连接字符串中可能会影响您的应用程序的安全性。 关更多信息,请参见保护连接信息。C#VBImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.Draw

45、ingImports System.Data.SqlClientImports System.Windows.Forms'This form dem on strates using a Bindin gNavigator to display 'rows from a database query seque ntially.Public Class Form1In heritsForm'This is the Bindin gNavigator that allows the user' to navigate through the rows in a D

46、ataSet.Private customersBindingNavigator As NewBindingNavigator( True)' This is the BindingSource that provides data for' the Textbox control.Private customersBindingSource As NewBindingSource()' This is the TextBox control that displays the CompanyName' field from the the DataSet.Pr

47、ivate companyNameTextBoxAs NewTextBox()Public Sub New()' Set up the BindingSource component.Me.customersBindingNavigator.BindingSource =Me.customersBindingSourceMe.customersBindingNavigator.Dock = DockStyle.TopMe.Controls.Add( Me.customersBindingNavigator)' Set up the TextBox control for dis

48、playing company names. MpanyNameTextBox.Dock = DockStyle.Bottom Me.Controls.Add( MpanyNameTextBox)' Set up the form.Me.Size = NewSize(800, 200)AddHandler Me.Load, AddressOf Form1_LoadEnd Sub 'NewPrivate Sub Form1_Load(ByVal sender As Object , ByVal e As EventArgs)' Open a connection to t

49、he database.' Replace the value of connectString with a valid' connection string to a Northwind database accessible' to your system.Dim connectString As String = _"Integrated Security=SSPI;Persist Security Info=False;" & _"Initial Catalog=Northwind;Data Source=localhos

50、t"Dim connection As NewSqlConnection(connectString)TryDim dataAdapter1 As NewSqlDataAdapter( _ NewSqlCommand"(Select * From Customers" , connection)Dim ds As NewDataSet( "Northwind Customers") ds.Tables.Add("Customers")dataAdapter1.Fill(ds.Tables("Customers&qu

51、ot;)'Assign the DataSet as the DataSource for the BindingSource.MecustomersBi ndi ngSource.DataSource = ds.Tables( "Customers")'Bind the Compa ny Name field to the TextBox con trol.MecompanyNameTextBox.DataBindings.Add( NewBinding( "Text"MecustomersBindingSource,"Com

52、panyName" True)Fi nallyconn ecti on .Dispose。End TryEnd Sub'Form1_Load<STAThread()> _Public Shared Sub Main()Applicatio n.En ableVisualStyles()Applicatio n.Ru n( NewForm1()End SubEnd Class如何:向 Windows窗体BindingNavigator 控件添加“加载”、“保存”和“取消” 按钮.NET Framework 4.5其他版本.7.8.9.此代码最有可能位于

53、窗体的此主题尚未评级 - 评价此主题BindingNavigator 控件是一个具有特殊用途的 ToolStrip 控件,用于定位和操作窗体上绑定到 数据的控件。由于 BindingNavigator 组件是一个 ToolStrip 控件,因此可以轻松对其进行修改,加入供用 户使用的附加或替代命令。在下面的过程中, 我们将 TextBox 控件绑定到数据, 并对 ToolStrip 控件进行修改, 在其中包 括“加载”、“保存”和“取消”按钮。向 BindingNavigator 组件添加 “加载 ”、 “保存 ”和“取消 ”按钮1. 向窗体中添加一个 TextBox 控件。2. 将其绑定到一个 BindingSource ,后者绑定到了一个数据源。在此例中,BindingSource 绑定到数据库。生成数据集和表适配器后,向窗体中拖一个 BindingNavigator 控件。将 BindingNavigator 控件的 BindingSource 属性设置为窗体上绑定到控件的 BindingSource 。选择 BindingNavigator 控件。单击智能标记符号 () ,“ BindingNavigator 任务” 对话框将出现,选

温馨提示

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

评论

0/150

提交评论