|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622 。
您需要 登录 才可以下载或查看,没有帐号?注册
x
UltraWinGrid Data Sources and Grid Rows
The information in this article applies to:
UltraWinGrid (v1.0.5005)
Summary
Many projects use the UltraWinGrid to navigate a sorted result set and standard Text Boxes to edit the data. To make this work there needs to be a known relationship between DataSources and Grid Rows.
For flat data this is not much of a problem since the UltraGridRow's Index property points directly to the position of the row in the bound DataTable. However, when working with Hierarchical data it becomes a little more complex.
Additional Information
Questions
How do I bind DataTable Rows to text boxes and change the position in the DataTable when the user selects rows in the UltraWinGrid?
Solution
This project uses the Selected.Rows(0).Index property as a direct pointer to the DataTable Rows Item for Band 0.
Retrieving a Row Item pointer to hierarchical bands within the grid is a little more difficult as illustrated in the code review.
Step-By-Step Example
This sample project allows the user to select rows in the UltraGrid and the corresponding data display in TextBoxes:
This project consists of the following classes:
clsCustomerData exposes a method used to create the Customers DataTable. This class is not reviewed.
clsCustOrderData exposes a method used to create the Customer/Orders DataSet. This class is not reviewed.
clsOrderData exposes a method used to create the Orders DataTable. This class is not reviewed.
Form1 contains all code relevant to this project and consists of the following Code Regions:
Form Events
The Form Events Region contains the following event handlers:
MyBase.Load - The Form Load event code creates the Customers/Orders DataSet and binds it to UltraGrid, then columns from the Customers and Orders tables are bound to the text box controls:
' create and bind DataSet
Dim dataCustOrder As New clsCustOrderDataSet()
Dim aDataSet As DataSet = dataCustOrder.MakeCustOrderDataSet
UltraGrid1.DataSource = aDataSet
' bind Customers DataTable Text Boxes
txtCustomerID.DataBindings.Add("Text", aDataSet.Tables("Customers"), "CustomerID")
txtCustomerName.DataBindings.Add("Text", aDataSet.Tables("Customers"), "CustomerName")
txtLastOrderDate.DataBindings.Add("Text", aDataSet.Tables("Customers"), "LastOrderDate")
' bind Orders DataTable Text Boxes
txtOrderID.DataBindings.Add("Text", aDataSet.Tables("Orders"), "OrderID")
txtEmployeeID.DataBindings.Add("Text", aDataSet.Tables("Orders"), "EmployeeID")
txtOrderDate.DataBindings.Add("Text", aDataSet.Tables("Orders"), "OrderDate")
UltraGrid Events
The UltraGrid Events Region contains the following event handlers:
UltraGrid1.InitializeLayout - The InitializeLayout event handler sets the Row Select Type and Cell Click Action properties:
e.Layout.Override.SelectTypeRow = Infragistics.Win.UltraWinGrid.SelectType.Single
' UltraGrid 的整行选取
e.Layout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect
UltraGrid1.AfterSelectChange - The AfterSelectChange event fires each time the selected row in the grid changes.
The band index and row index of the Selected Row display in the text boxes:
' display the selected row band and row indexes With UltraGrid1
If .Selected.Rows.Count > 0 Then
txtBand.Text = .Selected.Rows(0).Band.Index.ToString
txtRowIndex.Text = .Selected.Rows(0).Index.ToString
End If
End With
Extract a reference to the DataSet from the DataSource of the UltraGrid:
' Set selected row in Text Boxes Dim aDataSet As DataSet = UltraGrid1.DataSource
Dim aDataRow As DataRow
Setup a Select construct to deal with selections on band 0 and band 1:
Select Case UltraGrid1.Selected.Rows(0).Band.Index
For selections in Band 0 the CurrencyManager can be called directly with the Selected.Rows(0).Index to position the record in the TextBoxes, and turn off the text boxes for Band 1 because they are not relevant if a row in band 0 is selected:
Case 0
' Use CurrencyManager to set position of Band 0
Dim currencymanagerCustomers As CurrencyManager
currencymanagerCustomers = Me.BindingContext(aDataSet.Tables("Customers"))
currencymanagerCustomers.Position = UltraGrid1.Selected.Rows(0).Index
' turn off band 1 text boxes
lblBand1.Visible = False
txtOrderID.Visible = False
txtEmployeeID.Visible = False
txtOrderDate.Visible = False
The Band 0 record can be directly positioned by setting the CurrencyManager.Position property to the row index of the ParentRow:
Case 1
' Use Currency Manager to set position of Band 0
Dim currencymanagerCustomers As CurrencyManager
currencymanagerCustomers = Me.BindingContext(aDataSet.Tables("Customers"))
currencymanagerCustomers.Position = UltraGrid1.Selected.Rows(0).ParentRow.Index
Positioning the Orders DataTable (Band 1) is a little more difficult. The Selected.Rows(0).Index property provides the index of the rows relative to the parent row, not relative to all rows in the DataTable. The following code locates a DataRow in the “Orders” DataTable that corresponds to the Selected Row in the UltraGrid:
' Find Row in DataTable for Selected Row
Dim rowIndex As Integer = UltraGrid1.Selected.Rows(0).Index
Dim parentIndex As Integer = UltraGrid1.Selected.Rows(0).ParentRow.Index
Dim parentRow As DataRow = aDataSet.Tables("Customers").Rows(parentIndex)
Dim childrows() As DataRow = parentRow.GetChildRows("CustOrder")
Dim childRow As DataRow = childrows(rowIndex)
With an object reference to the DataTable Row (childRow) the next challenge is to find the position of this row within the Orders DataTable. There should be a simple way to find the row index of a row in a DataTable, but this author was unable to find one. So, if all else fails, loop through the rows of the Orders DataTable until the rowIndex is found:
' Find rowIndex of Row in DataTable
For rowIndex = 0 To aDataSet.Tables("Orders").Rows.Count - 1
If childRow Is aDataSet.Tables("Orders").Rows(rowIndex) Then
Exit For
End If
Next
Use the Currency Manager to position the Orders DataTable record into the text boxes and turn the band 1 text boxes on:
' Use Currency Manager to set position of Band 1
Dim currencymanagerOrders As CurrencyManager
currencymanagerOrders = Me.BindingContext(aDataSet.Tables("Orders"))
currencymanagerOrders.Position = rowIndex
' turn on band 1 text boxes
lblBand1.Visible = True
txtOrderID.Visible = True
txtEmployeeID.Visible = True
txtOrderDate.Visible = True
End Select
Review
This is a rather interesting project illustrating the relationship between UltraGrid Row Indexes and the Underlying DataTable Rows. Also illustrated is the mechanism used in VB.NET to bind DataTable columns to TextBoxes.
Samples
ultrawingrid_tutorial_data_sources_and_grid_rows.zip
UltraWinGrid Data Sources and Grid Rows |
|