.NET 2.0 - WinForm Control - DataGridView 数据绑定
在应用开发中常常用到这样的应用: DataGridView用来显示全体数据,还有由一些TextBox控件来提供修改功能。这里如果用挨个赋值的办法就不好了。.NET中提供的一个重要 的技术就是——数据绑定。数据绑定中几个重要对象及属性有:Control.DataSource属性,BindingSource对 象,Form.BindingContext属性。
这里通过一个例子来说明WinForm开发中如何使用数据绑定。首先有数据结构形如:(这里使用NorthWind的Sample数据。)
Customers和Orders数据是1:n的关系,Orders和OrdersDetails数据是1:n的关系。
因此设计Form如图,ListView绑定Custormers数据,入力控件绑定Customers里数据的其他字段。OrdersDataGridView绑定Orders数据,DetaiDataGridView绑定Order Details数据。
首先通过DataSet的DataRelation建立三表的关系:
If objConn Is Nothing Then Return
objDataSet = New DataSet("Data")
Dim strSql As String = "select * from customers"
Dim objDbAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSql, objConn)
objDbAdapter.Fill(objDataSet, "Customers")
objDbAdapter.SelectCommand.CommandText = "select * from orders"
objDbAdapter.Fill(objDataSet, "Orders")
objDbAdapter.SelectCommand.CommandText = "select * from [order details]"
objDbAdapter.Fill(objDataSet, "OrderDetails")
objDataSet.Relations.Add("CustomerOrder", objDataSet.Tables("Customers").Columns("CustomerID"), objDataSet.Tables("Orders").Columns("CustomerID"))
objDataSet.Relations.Add("OrderDetail", objDataSet.Tables("Orders").Columns("OrderID"), objDataSet.Tables("OrderDetails").Columns("OrderID"))
End Sub
通过BindingSource绑定到DataGridView上,需要注意的是比较.NET 1.1的绑定,这里用BindingSource取代,在创建DataGridViewOrders的BindingSource对象的 DataSource使用的是customBindingSource,而DataMember是Customers表和Orders表的 DataRelation的名字——CustomerOrder。
orderBindingSource.DataSource = customBindingSource
orderBindingSource.DataMember = "CustomerOrder"
同理Detail的BindingSource对象的DataSource使用的是orderBindingSource,而DataMember是Orders表和Order Details表的DataRelation的名字——OrderDetail。
detailBindingSource.DataSource = orderBindingSource
detailBindingSource.DataMember = "OrderDetail"
If objDataSet Is Nothing Then Return
Dim customBindingSource As BindingSource = New BindingSource
Dim orderBindingSource As BindingSource = New BindingSource
Dim detailBindingSource As BindingSource = New BindingSource
customBindingSource.DataSource = objDataSet
customBindingSource.DataMember = "Customers"
orderBindingSource.DataSource = customBindingSource
orderBindingSource.DataMember = "CustomerOrder"
detailBindingSource.DataSource = orderBindingSource
detailBindingSource.DataMember = "OrderDetail"
Me.lstCustom.DataSource = customBindingSource
Me.lstCustom.ValueMember = "ContactName"
Me.lstCustom.DisplayMember = "ContactName"
Me.DataGridView1.DataSource = orderBindingSource
Me.DataGridView2.DataSource = detailBindingSource
Me.DataGridView1.AutoResizeColumns()
Me.DataGridView2.AutoResizeColumns()
Me.txtName.DataBindings.Add("Text", customBindingSource, "ContactName")
Me.txtContactTitle.DataBindings.Add("Text", customBindingSource, "ContactTitle")
Me.txtAddress.DataBindings.Add("Text", customBindingSource, "Address")
Me.txtCity.DataBindings.Add("Text", customBindingSource, "City")
Me.txtRegion.DataBindings.Add("Text", customBindingSource, "Region")
Me.txtPostalCode.DataBindings.Add("Text", customBindingSource, "PostalCode")
Me.txtPhone.DataBindings.Add("Text", customBindingSource, "Phone")
Me.txtFax.DataBindings.Add("Text", customBindingSource, "Fax")
Me.txtCountry.DataBindings.Add("Text", customBindingSource, "Country")
End Sub
而浏览数据,可以通过BindingManagerBase的Position来控制:
'Dim binding As Binding = Me.txtName.DataBindings("Text")
'Dim manager As BindingManagerBase = binding.BindingManagerBase
Dim manager As BindingManagerBase = Me.BindingContext(Me.lstCustom.DataSource)
manager.Position = 0
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Dim manager As BindingManagerBase = Me.BindingContext(Me.lstCustom.DataSource)
If manager.Position > 0 Then
manager.Position -= 1
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim manager As BindingManagerBase = Me.BindingContext(Me.lstCustom.DataSource)
If manager.Position < manager.Count - 1 Then
manager.Position += 1
End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Dim manager As BindingManagerBase = Me.BindingContext(Me.lstCustom.DataSource)
manager.Position = manager.Count - 1
End Sub