Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

TextBox Lazy Binding

0.00/5 (No votes)
16 Aug 2009 1  
I feel lazy when need to bind data to TextBox

Download LazyBinding.zip - 37.95 KB

Introduction 

This code used to bind data from DataTable to TextBox automatically.  

Background

Due to my company's software development need to bind data to controls manually.

When I am working. It feel like pain in my ...

So I think it should be the way to make it easy for me. Then I try it out

Using the code 

This code is very simple. It's compare controls name and field name.

Condition

Control name need to be the same as field/column name.  With my company design we using prefix to tell type of control for control id such as "txtData1" (txt for TextBox)

In this code it have 4 TextBox

- txtData1 

- txtData2

- txtData3

- txtData4 

So I need to replace "txt" with "" when try to bind data to TextBox

In DataTable I added 4 Column

- Data1 

- Data2

- Data3

- Data4

This is the key code.
        '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 

Form load I add some demo data and bind for first load.

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 I use it for navigate back.

    '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 I use it for navigate Next.

    '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		   

But I cannot use this code with my company software.

Because it doesn't follow my company software design.

And it may be confuse another developer in my company.

Points of Interest   

This code may be useful for another project or may applied for anything else.

Even if it's looks like crazy or useless but I think it's good for me to try.

History 

14/08/2009 23:47 

This is my first article I just want to share what I interested.

Don't be hesitate to tell if you have any advice

PS. Sorry for my bad Grammar.  

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here