看有人问到了,就把之前写的代码拿出来跟大家分享下。
事件响应写在ItemEvent中,鼠标双击事件。
Public Sub SortMatrix(ByVal pval As SAPbouiCOM.ItemEvent)
Dim DT As DataTable
Dim DTCol As System.Data.DataColumn
Dim DTRow As System.Data.DataRow
Dim DTRowV As System.Data.DataRowView
Dim oForm As SAPbouiCOM.Form
Dim oDBDS As SAPbouiCOM.DBDataSource
Dim oItem As SAPbouiCOM.Item
Dim oMatrix As SAPbouiCOM.Matrix
Dim oCol As SAPbouiCOM.Column
Dim sKeyColField As String '排序的字段绑定的Datasource字段名
Dim iRow As Integer
Dim iLineId As Integer
Dim iDSIndex As Integer
Dim objCompareValue_1 As Object
Dim objCompareValue_2 As Object
Dim dtDataType As System.Type
Try
oForm = SBOApp.Forms.Item(pval.FormUID)
oItem = oForm.Items.Item(pval.ItemUID)
If oItem.Type <> SAPbouiCOM.BoFormItemTypes.it_MATRIX Then Exit Sub
DT = New System.Data.DataTable
DTCol = New System.Data.DataColumn
oMatrix = oForm.Items.Item(pval.ItemUID).Specific
If oMatrix.RowCount < 2 Then Exit Sub '小于2行,无需排序,退出
SBOApp.SetStatusBarMessage("正在排序,请稍后。", SAPbouiCOM.BoMessageTime.bmt_Long, False)
oForm.Freeze(True)
oCol = oMatrix.Columns.Item(pval.ColUID)
sKeyColField = oCol.DataBind.Alias '排序依据的字段绑定的Datasource字段名
oMatrix.FlushToDataSource()
oDBDS = oForm.DataSources.DBDataSources.Item(oCol.DataBind.TableName)
dtDataType = Me.GetSystemDataTypeBySBODataType(oDBDS.Fields.Item(sKeyColField).Type)
If dtDataType Is System.Type.GetType("System.Int32") Then
If Replace(Me.GetMatrixCellValue(oCol, 1), " ", "") = "" Then
objCompareValue_1 = 0
Else
objCompareValue_1 = CInt(Me.GetMatrixCellValue(oCol, 1)) '第一行的数据
End If
If Replace(Me.GetMatrixCellValue(oCol, oMatrix.RowCount), " ", "") = "" Then
objCompareValue_2 = 0
Else
objCompareValue_2 = CInt(Me.GetMatrixCellValue(oCol, oMatrix.RowCount)) '最后一行的数据
End If
ElseIf dtDataType Is System.Type.GetType("System.Single") Then
If Replace(Me.GetMatrixCellValue(oCol, 1), " ", "") = "" Then
objCompareValue_1 = 0
Else
objCompareValue_1 = CSng(Me.GetMatrixCellValue(oCol, 1)) '第一行的数据
End If
If Replace(Me.GetMatrixCellValue(oCol, oMatrix.RowCount), " ", "") = "" Then
objCompareValue_2 = 0
Else
objCompareValue_2 = CSng(Me.GetMatrixCellValue(oCol, oMatrix.RowCount)) '最后一行的数据
End If
Else
objCompareValue_1 = Me.GetMatrixCellValue(oCol, 1).ToString '第一行的数据
objCompareValue_2 = Me.GetMatrixCellValue(oCol, oMatrix.RowCount).ToString '最后一行的数据
End If
'往DataTable中添加排序依据字段
DTCol.ColumnName = sKeyColField
DTCol.DataType = dtDataType
If DT.Columns.Contains(DTCol.ColumnName) Then
DTCol.DataType = dtDataType
Else
DT.Columns.Add(DTCol)
End If
'往DataTable中添加"ID"字段,记录DataSource序列
DTCol = New System.Data.DataColumn
DTCol.ColumnName = "ID"
DTCol.DataType = System.Type.GetType("System.Int32")
If DT.Columns.Contains(DTCol.ColumnName) Then
DTCol.DataType = System.Type.GetType("System.Int32")
Else
DT.Columns.Add(DTCol)
End If
'为DataTable添加行数据
For iRow = 0 To oDBDS.Size - 1
oDBDS.Offset = iRow
DTRow = DT.NewRow
DTRow(sKeyColField) = oDBDS.GetValue(sKeyColField, iRow)
DTRow("ID") = oDBDS.GetValue("LineId", iRow)
DT.Rows.Add(DTRow)
Next
'排序
If objCompareValue_1 > objCompareValue_2 Then '原来是降序排列
DT.DefaultView.Sort = sKeyColField
ElseIf objCompareValue_1 < objCompareValue_2 Then
DT.DefaultView.Sort = sKeyColField & " Desc "
Else
oForm.Freeze(False)
Exit Sub
End If
'根据排序后结果更新Matrix中值
oItem.AffectsFormMode = False
For iRow = 0 To DT.DefaultView.Count - 1
DTRowV = DT.DefaultView(iRow)
iLineId = DTRowV("ID")
'找到DataSource中的行
For iDSIndex = 0 To oDBDS.Size - 1
oDBDS.Offset = iDSIndex
If iLineId = oDBDS.GetValue("LineId", iDSIndex) Then
Exit For
End If
Next
oMatrix.SetLineData(iRow + 1)
Next
oItem.AffectsFormMode = True
'oMatrix.LoadFromDataSource()
SBOApp.SetStatusBarMessage("排序完成!", SAPbouiCOM.BoMessageTime.bmt_Medium, False)
oForm.Freeze(False)
Catch ex As Exception
oForm.Freeze(False)
Throw New Exception("排序发生错误." & ex.Message)
End Try
End Sub
'''
''' 根据BO的数据类型获取系统数据类型
'''
'''
'''
'''
Public Function GetSystemDataTypeBySBODataType(ByVal DataType As SAPbouiCOM.BoFieldsType) As System.Type
Try
Select Case DataType
Case SAPbouiCOM.BoFieldsType.ft_AlphaNumeric
Return System.Type.GetType("System.String")
Case SAPbouiCOM.BoFieldsType.ft_Date
Return System.Type.GetType("System.String")
Case SAPbouiCOM.BoFieldsType.ft_Float
Return System.Type.GetType("System.Single")
Case SAPbouiCOM.BoFieldsType.ft_Integer
Return System.Type.GetType("System.Int32")
Case Else
Return System.Type.GetType("System.String")
End Select
Catch ex As Exception
Return System.Type.GetType("System.String")
End Try
End Function
'''
''' 得到Matrix单元格的值
'''
''' 列
''' 行
'''
'''
Public Function GetMatrixCellValue(ByVal Column As SAPbouiCOM.Column, ByVal Row As Integer) As Object
Dim edt As SAPbouiCOM.EditText
Dim chk As SAPbouiCOM.CheckBox
Dim cmb As SAPbouiCOM.ComboBox
Dim oObject As Object
Try
Select Case Column.Type
Case SAPbouiCOM.BoFormItemTypes.it_CHECK_BOX
chk = Column.Cells.Item(Row).Specific
If chk.Checked Then Return "Y" Else Return "N"
Case SAPbouiCOM.BoFormItemTypes.it_EDIT, SAPbouiCOM.BoFormItemTypes.it_LINKED_BUTTON
edt = Column.Cells.Item(Row).Specific
Return edt.Value
Case SAPbouiCOM.BoFormItemTypes.it_COMBO_BOX
cmb = Column.Cells.Item(Row).Specific
Try
oObject = cmb.Selected.Value
Catch ex As Exception
End Try
Return oObject
End Select
Catch ex As Exception
Throw New Exception
End Try
End Function
SBO二次开发中点击Matrix第一行的排序功能代码
最新推荐文章于 2015-01-26 13:14:13 发布