Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Visual Basic

TextBox Lazy Binding

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Aug 2009CPOL1 min read 17.7K   128   5   2
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.
VB.NET
        '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.

VB.NET
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.

VB.NET
'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.

VB.NET
'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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Thailand Thailand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI'm doing something similar... Pin
JohannNutter20-Aug-09 16:31
JohannNutter20-Aug-09 16:31 
GeneralRe: I'm doing something similar... Pin
Karanig21-Aug-09 16:59
Karanig21-Aug-09 16:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.