这个示例,实现编辑MODBUS配置,供以后,实现类似功能参考

Imports System.Reflection


 
Public Class DlgModbusGauge

 
    ''' <summary>
    ''' 当前要配置的仪表的GID
    ''' </summary>
    ''' <remarks></remarks>
    Public mObjGID As Guid

 
    Private Sub DlgModbusGauge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bindDataSource()
        loadData()
    End Sub

 
    Private _modbusConfig As List(Of ModbusItem)

 
    Private _modbusConfigDeleted As New List(Of ModbusItem)

 
    Private Sub loadData()
        '导入Modbus的配置数据
        Dim pDA As New ModbusDBAccess
        _modbusConfig = pDA.mExtend.getList()
        If (_modbusConfig IsNot Nothing AndAlso _modbusConfig.Count > 0) Then
            xDataGridView.RowCount = _modbusConfig.Count + 1
        ElseIf (_modbusConfig Is Nothing) Then
            _modbusConfig = New List(Of ModbusItem)
        End If
    End Sub

 
    Private Sub bindDataSource()
        bindModbusField()
        bindFunction()
        bindModbusDataType()
        bindPrecision()
        bindUnitAdjust()
    End Sub

 
    Private _fieldList As New List(Of ValueObject(Of String))

 
    ''' <summary>
    ''' 绑定字段的可选项
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub bindModbusField()
        Dim pType As Type = GetType(GaugeData)
        Dim pPropertyList As PropertyInfo()

 
        pPropertyList = pType.GetProperties()
        If (pPropertyList.Length = 0) Then
            Return
        End If

 
        For Each p As PropertyInfo In pPropertyList
            If (p.CanRead AndAlso p.IsDefined(GetType(GaugeDataAttribute), True)) Then
                Dim pAttribute As GaugeDataAttribute = CType(Attribute.GetCustomAttribute(p, GetType(GaugeDataAttribute)), GaugeDataAttribute)
                If (pAttribute IsNot Nothing) Then
                    _fieldList.Add(New ValueObject(Of String)(pAttribute.mName, p.Name))
                End If
            End If
        Next

 
        Me.xColumnModbusField.DisplayMember = ValueObject(Of String).displayMemeber
        Me.xColumnModbusField.ValueMember = ValueObject(Of String).valueMember
        Me.xColumnModbusField.DataSource = _fieldList

 
    End Sub

 
    Private Sub bindFunction()
        Dim pFieldList As New List(Of ValueObject(Of Byte))
        pFieldList.Add(New ValueObject(Of Byte)("1", 1))
        pFieldList.Add(New ValueObject(Of Byte)("2", 2))
        pFieldList.Add(New ValueObject(Of Byte)("3", 3))
        pFieldList.Add(New ValueObject(Of Byte)("4", 4))

 
 
        Me.xColumnFunction.DisplayMember = ValueObject(Of Byte).displayMemeber
        Me.xColumnFunction.ValueMember = ValueObject(Of Byte).valueMember
        Me.xColumnFunction.DataSource = pFieldList
    End Sub

 
    Private Sub bindModbusDataType()

 
        Dim pFieldList As New List(Of ValueObject(Of EMModubsDataType))
        pFieldList.Add(New ValueObject(Of EMModubsDataType)("Int16", EMModubsDataType.emInt16))
        pFieldList.Add(New ValueObject(Of EMModubsDataType)("Int32", EMModubsDataType.emInt32))
        pFieldList.Add(New ValueObject(Of EMModubsDataType)("UInt16", EMModubsDataType.emUInt16))
        pFieldList.Add(New ValueObject(Of EMModubsDataType)("UInt32", EMModubsDataType.emUInt32))

 
 
        Me.xColumnDataType.DisplayMember = ValueObject(Of EMModubsDataType).displayMemeber
        Me.xColumnDataType.ValueMember = ValueObject(Of EMModubsDataType).valueMember
        Me.xColumnDataType.DataSource = pFieldList
    End Sub

 
    Private Sub bindPrecision()

 
        Dim pFieldList As New List(Of ValueObject(Of Double))
        pFieldList.Add(New ValueObject(Of Double)("1", 1.0))
        pFieldList.Add(New ValueObject(Of Double)("10", 10.0))
        pFieldList.Add(New ValueObject(Of Double)("100", 100.0))
        pFieldList.Add(New ValueObject(Of Double)("1000", 1000.0))
        pFieldList.Add(New ValueObject(Of Double)("10000", 10000.0))

 
 
        Me.xColumnPrecision.DisplayMember = ValueObject(Of EMModubsDataType).displayMemeber
        Me.xColumnPrecision.ValueMember = ValueObject(Of EMModubsDataType).valueMember
        Me.xColumnPrecision.DataSource = pFieldList
    End Sub

 
 
    Private Sub bindUnitAdjust()

 
        Dim pFieldList As New List(Of ValueObject(Of Double))
        pFieldList.Add(New ValueObject(Of Double)("0.0001", 0.0001))
        pFieldList.Add(New ValueObject(Of Double)("0.001", 0.001))
        pFieldList.Add(New ValueObject(Of Double)("0.01", 0.01))
        pFieldList.Add(New ValueObject(Of Double)("0.1", 0.1))
        pFieldList.Add(New ValueObject(Of Double)("1", 1.0))
        pFieldList.Add(New ValueObject(Of Double)("10", 10.0))
        pFieldList.Add(New ValueObject(Of Double)("100", 100.0))
        pFieldList.Add(New ValueObject(Of Double)("1000", 1000.0))
        pFieldList.Add(New ValueObject(Of Double)("10000", 10000.0))

 
 
        Me.xColumnUnitAdjust.DisplayMember = ValueObject(Of Double).displayMemeber
        Me.xColumnUnitAdjust.ValueMember = ValueObject(Of Double).valueMember
        Me.xColumnUnitAdjust.DataSource = pFieldList
    End Sub

 
 
    Private Sub DataGridViewX1_CellValueNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValueNeeded
        If (e.RowIndex < 0 OrElse e.RowIndex >= _modbusConfig.Count) Then
            Return
        End If
        Select Case e.ColumnIndex
            Case xColumnDataType.Index
                e.Value = _modbusConfig(e.RowIndex).mDataType
            Case xColumnFunction.Index
                e.Value = _modbusConfig(e.RowIndex).mModbusFunction
            Case xColumnModbusField.Index
                e.Value = _modbusConfig(e.RowIndex).mModbusField
            Case xColumnObjGID.Index
                e.Value = _modbusConfig(e.RowIndex).mObjGID
            Case xColumnPrecision.Index
                e.Value = _modbusConfig(e.RowIndex).mPrecision
            Case xColumnRemark.Index
                e.Value = _modbusConfig(e.RowIndex).mRemark
            Case xColumnUnitAdjust.Index
                e.Value = _modbusConfig(e.RowIndex).mUnitAdjust
            Case xColumnValueAddress.Index
                e.Value = _modbusConfig(e.RowIndex).mValueAddress
        End Select
    End Sub

 
    Private Sub DataGridViewX1_CellValuePushed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValuePushed
        If (e.RowIndex < 0 OrElse e.RowIndex >= _modbusConfig.Count) Then
            Return
        End If
        Select Case e.ColumnIndex
            Case xColumnDataType.Index
                _modbusConfig(e.RowIndex).mDataType = e.Value
                '自动生成地址数据
                If (e.RowIndex > 0) Then
                    Select Case _modbusConfig(e.RowIndex).mDataType
                        Case EMModubsDataType.emInt16, EMModubsDataType.emUInt16
                            _modbusConfig(e.RowIndex).mValueAddress = _modbusConfig(e.RowIndex - 1).mValueAddress + 1
                        Case Else
                            _modbusConfig(e.RowIndex).mValueAddress = _modbusConfig(e.RowIndex - 1).mValueAddress + 2
                    End Select
                End If
            Case xColumnFunction.Index
                _modbusConfig(e.RowIndex).mModbusFunction = e.Value
            Case xColumnModbusField.Index
                _modbusConfig(e.RowIndex).mModbusField = e.Value
            Case xColumnObjGID.Index
                _modbusConfig(e.RowIndex).mObjGID = e.Value
            Case xColumnPrecision.Index
                _modbusConfig(e.RowIndex).mPrecision = e.Value
            Case xColumnRemark.Index
                _modbusConfig(e.RowIndex).mRemark = e.Value
            Case xColumnUnitAdjust.Index
                _modbusConfig(e.RowIndex).mUnitAdjust = e.Value
            Case xColumnValueAddress.Index
                _modbusConfig(e.RowIndex).mValueAddress = e.Value
        End Select

 
        If (_modbusConfig(e.RowIndex).mRowState = DataRowState.Detached) Then
            _modbusConfig(e.RowIndex).mRowState = DataRowState.Added
        ElseIf (_modbusConfig(e.RowIndex).mRowState = DataRowState.Unchanged) Then
            _modbusConfig(e.RowIndex).mRowState = DataRowState.Modified
        End If
    End Sub

 
    Private Sub DataGridViewX1_NewRowNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles xDataGridView.NewRowNeeded
        '在数据表中增加一行
        Dim pData As New ModbusItem
        With pData
            .mDataType = EMModubsDataType.emInt32
            .mLastUser = MyFacade.mBase.mUserName
            .mModbusField = _fieldList(0).mValue
            .mModbusFunction = 4
            .mObjGID = mObjGID
            .mPrecision = 1.0
            .mRemark = ""
            .mRowState = DataRowState.Detached
            .mUnitAdjust = 1.0
            .mUpdateTime = DateTime.Now
            .mValueAddress = 1
        End With
        _modbusConfig.Add(pData)
    End Sub

 
    Private Sub DataGridViewX1_UserDeletingRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles xDataGridView.UserDeletingRow
        If (_modbusConfig.Count > e.Row.Index) Then
            Dim pItem As ModbusItem = _modbusConfig(e.Row.Index)
            If (pItem.mRowState = DataRowState.Modified OrElse pItem.mRowState = DataRowState.Unchanged) Then
                '如果是数据库中本身已存在的,则需要从数据库中也删除
                '删除的办法,是首先移除到临时列表中,然后按保存时,提交到数据库中删除
                pItem.mRowState = DataRowState.Deleted
                _modbusConfigDeleted.Add(pItem)
            End If
            _modbusConfig.RemoveAt(e.Row.Index)
        End If
    End Sub

 
    'Private Sub xDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xDelete.Click
    ' If (MsgBox("确定要删除所选行吗?", MsgBoxStyle.YesNo, Application.ProductName) = MsgBoxResult.No) Then
    ' Return
    ' End If

 
    ' 'For p1 As Integer = xDataGridView.SelectedRows.Count To 0 Step -1
    ' ' xDataGridView.Rows.Remove()
    ' 'Next
    ' For Each p1 As DataGridViewRow In xDataGridView.SelectedRows
    ' xDataGridView.
    ' Next
    'End Sub

 
    Private Sub xSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xSave.Click
        Dim pDA As New ModbusDBAccess
        pDA.submit(_modbusConfig) '提交更新
        If (_modbusConfigDeleted IsNot Nothing AndAlso _modbusConfigDeleted.Count > 0) Then
            '提交删除项
            pDA.submit(_modbusConfigDeleted)
            _modbusConfigDeleted.Clear()
        End If
    End Sub

 
    Private Sub xClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xClose.Click
        Me.DialogResult = Forms.DialogResult.OK
    End Sub
End Class