65.9K
CodeProject 正在变化。 阅读更多。
Home

TextBox 延迟绑定

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2009 年 8 月 17 日

CPOL

1分钟阅读

viewsIcon

18379

downloadIcon

128

当需要将数据绑定到 TextBox 时,我感到很懒


下载 LazyBinding.zip - 37.95 KB

介绍 

这段代码用于自动将 DataTable 中的数据绑定到 TextBox。

背景

由于我公司软件开发需要手动绑定数据到控件。

当我工作时,感觉...很痛苦。

所以我认为应该有一种方法来让我更容易地完成它。然后我尝试了一下。

使用代码 

这段代码非常简单。它比较控件名称和字段名称。

条件

控件名称需要与字段/列名称相同。根据我公司设计,我们使用前缀来指示控件类型,例如 "txtData1"(txt 代表 TextBox)。

在这段代码中,有 4 个 TextBox

- txtData1

- txtData2

- txtData3

- txtData4

因此,在尝试将数据绑定到 TextBox 时,我需要将 "txt" 替换为空字符串。

在 DataTable 中,我添加了 4 列

- Data1

- Data2

- Data3

- Data4

这是关键代码。
        'This snipet will loop all control in form 
        'But if you have GroupBox or any container 
        'You have to check. if it any container you need to loop in that container too.
        For Each ctrl As Control In Me.Controls
            Try
                'Binding data to TextBox 
                'By Replace txt with blank 
                'Example we loop to txtData1.Text = dt.Rows(0)("txtData1".Replace("txt", ""))                             
                ctrl.Text = dt.Rows(0)(ctrl.Name.Replace("txt", ""))
            Catch ex As Exception
                'just ignore it if it cannot bind 
                '(Any control name that replace "txt" with "" and not match with field/column name)
            End Try
        Next 

表单加载时,我添加了一些演示数据并进行首次绑定。

Public Class Form1
    'Declare DataTable
    Dim dt As DataTable

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.dt = New DataTable

        'Add Column to DataTable
        Me.dt.Columns.Add("Data1")
        Me.dt.Columns.Add("Data2")
        Me.dt.Columns.Add("Data3")
        Me.dt.Columns.Add("Data4")
        Me.dt.AcceptChanges()

        'Insert Some Data To DataTable
        For i As Integer = 0 To 10
            Dim dr As DataRow = Me.dt.NewRow
            dr("Data1") = i
            dr("Data2") = i & i
            dr("Data3") = i & i & i
            dr("Data4") = i & i & i & i
            dt.Rows.Add(dr)
        Next

        'This snipet will loop all control in form 
        'But if you have GroupBox or any container 
        'You have to check. if it any container you need to loop in that container too.
        For Each ctrl As Control In Me.Controls
            Try
                'Binding data to TextBox 
                'By Replace txt with blank 
                'Example we loop to txtData1.Text = dt.Rows(0)("txtData1".Replace("txt", ""))                             
                ctrl.Text = dt.Rows(0)(ctrl.Name.Replace("txt", ""))
            Catch ex As Exception
                'just ignore it if it cannot bind 
                '(Any control name that replace "txt" with "" and not match with field/column name)
            End Try
        Next
        'Set label text to current row number
        Me.lblRecrodNumber.Text = 0
    End Sub 

btnBack 用于返回。

    'Navigate Back
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        'Get current row and minus for navigate back
        Dim curRow As Integer = CInt(Me.lblRecrodNumber.Text) - 1
        If curRow > 0 Then
            For Each ctrl As Control In Me.Controls
                Try
                    ctrl.Text = dt.Rows(curRow)(ctrl.Name.Replace("txt", ""))
                Catch ex As Exception

                End Try
            Next
            'Set label text to current row number        
            Me.lblRecrodNumber.Text = curRow
        Else
            MessageBox.Show("Already at first record.")
        End If
    End Sub 

btnNext 用于前进。

    'Navigate Next
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        'Get current row and plus for navigate next
        Dim curRow As Integer = CInt(Me.lblRecrodNumber.Text) + 1
        If curRow < Me.dt.Rows.Count Then
            For Each ctrl As Control In Me.Controls
                Try
                    ctrl.Text = dt.Rows(curRow)(ctrl.Name.Replace("txt", ""))
                Catch ex As Exception

                End Try
            Next
            'Set label text to current row number
            Me.lblRecrodNumber.Text = curRow
        Else
            MessageBox.Show("Already at last record.")
        End If
    End Sub		   

但我无法在公司软件中使用这段代码。

因为它不符合我公司软件的设计规范。

并且可能会让公司里的其他开发者感到困惑。

关注点   

这段代码可能对其他项目有用,或者可以应用于其他方面。

即使它看起来很疯狂或没用,但我认为对我尝试一下是好的。

历史 

14/08/2009 23:47 

这是我的第一篇文章,我只是想分享我对什么感兴趣的东西。

如果您有任何建议,请随时提出。

PS. 对不起我的语法不好。

© . All rights reserved.