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.
For Each ctrl As Control In Me.Controls
Try
ctrl.Text = dt.Rows(0)(ctrl.Name.Replace("txt", ""))
Catch ex As Exception
End Try
Next
Form load I add some demo data and bind for first load.
Public Class Form1
Dim dt As DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.dt = New DataTable
Me.dt.Columns.Add("Data1")
Me.dt.Columns.Add("Data2")
Me.dt.Columns.Add("Data3")
Me.dt.Columns.Add("Data4")
Me.dt.AcceptChanges()
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
For Each ctrl As Control In Me.Controls
Try
ctrl.Text = dt.Rows(0)(ctrl.Name.Replace("txt", ""))
Catch ex As Exception
End Try
Next
Me.lblRecrodNumber.Text = 0
End Sub
btnBack I use it for navigate back.
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
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
Me.lblRecrodNumber.Text = curRow
Else
MessageBox.Show("Already at first record.")
End If
End Sub
btnNext I use it for navigate Next.
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
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
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.