posts - 10, comments - 3, trackbacks - 0, articles - 0

批量赋值

Posted on 2007-09-07 13:21 石头 阅读(290) 评论(0)  编辑 收藏 引用 所属分类: .NET技术

由于项目中输入文本框居多,所以赋值有时很不方便,为了把实体赋值给相应的文本框,做了一个方法:

Public Shared Sub BatchSetData(ByRef ctl As Control, ByRef entity As Object, ByVal prefixControl As String)
        Dim ctlName As String = ""
        '若界面上是TableLayoutPanel 
        If TypeOf (ctl) Is TableLayoutPanel Then
            Dim objTableLayout As TableLayoutPanel = CType(ctl, TableLayoutPanel)
            Dim objType As Type = entity.GetType()

            Dim proInfo() As Reflection.PropertyInfo
            proInfo = objType.GetProperties()

            Dim _label As Label

            '遍历界面上Label的控件
            For i As Integer = 0 To proInfo.Length - 1
                ctlName = proInfo(i).Name.ToString() + prefixControl
                _label = CType(objTableLayout.Controls.Item(ctlName), Label)
                '根据实体中不同的类型完成赋值
                If _label IsNot Nothing Then
                    _label.Text = proInfo(i).GetValue(entity, Nothing)
                End If
            Next
        End If
    End Sub

只有注册用户登录后才能发表评论。