Bài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 7: VB.NET DataBinDing, kiểm soát nhập liệu

™Mục tiêu của bài:Sửdụng đư ng được các DataTable,

DataView, DataSet, DataRelation, kết hợp với

BindingSource, BingdingNavigator. để:

¾Quản lý vàLiên kết dữliệu vào các Control có

thuộc tính (Properties) Dadabindingthông dụng

như như:

9DataGridView,

9TextBox, Combobox

9DateTimePicker

9RadioButton

9CheckBox

pdf20 trang | Chuyên mục: Visual Basic 6.0 | Chia sẻ: dkS00TYs | Lượt xem: 2298 | Lượt tải: 2download
Tóm tắt nội dung Bài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 7: VB.NET DataBinDing, kiểm soát nhập liệu, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
taAdapter
9Cot là tên cột (Column) thuộc table Bang
815Windows Form programming with VB.Net 2005.
Binding to DateTimePicker
™ Cú pháp:
controlTên.DataBindings.Add(“Value”,ds, “Bang.Cot”)
Trong đó:
¾ controlTên: Tên DateTimePicker
¾ ds: DataSet đã được fill từ DataAdapter trước đó
¾Bang.Cot:
9 Bang là tên table đã được Fill vào DataSet từ
DataAdapter
9 Cot là tên cột (Column) thuộc table Bang
16Windows Form programming with VB.Net 2005.
Binding to RadioButton & CheckBox
™ Cú pháp:
Tên.DataBindings.Add(“Checked”,ds, “Bang.Cot”)
---------------------------------------------------------
™ Ghi chú:
Tất cả các control có chức năng DataBindings đều có 
phương thức Clear để xóa tất cả các dữ liệu đã Binding 
trước đó (thường dùng chức năng này để reset lại dữ
liệu). Trước khi dùng Databindings.Add ta nên dùng 
phương thức Clear trước mỗi control cần Binding.
™ Cú pháp:
controlTên.DataBindings.Clear()
917Windows Form programming with VB.Net 2005.
BindingManagerBase
™BindingManager: quản lý tất cả các đối tượng được 
gắn kết (Binding) dữ liệu trên cùng một DataSoure
và DataMember thông qua đối tượng 
BindingContext
™ Khai báo:
Dim TênBiến As BindingManagerBase
TênBiến = Me.BindingContext(ds, “Table”)
Trong đó:
* ds: là 1 Dataset đã được fill từ DataAdapter
* Table: là tên bảng đã được fill trong biến ds
18Windows Form programming with VB.Net 2005.
Thuộc tính của BindingManagerBase
™ Count: Trả về số dòng (Row) dữ liệu được quản 
lý bởi BindingManagerBase 
™Position: Get or Set vị trí hiện tại của dòng dữ
liệu hiện hành. Nghĩa là khi các TextBox, 
DateTimePicker, Combobox… đã được Binding 
khi Position thay đổi thì các dữ liệu hiển thị trên 
các đối tượng này cũng thay đổi theo. Vị trí đầu 
tiên được tính từ 0->count-1
10
19Windows Form programming with VB.Net 2005.
20Windows Form programming with VB.Net 2005.
VD: Binding dữ liệu vào các TextBox
‘ Gọi phương thức này trước Databindings.Add
Private Sub ClearBinDingTextBox()
txtLoai.DataBindings.Clear()
txtMa.DataBindings.Clear()
txtTen.DataBindings.Clear()
txtDVT.DataBindings.Clear()
txtGia.DataBindings.Clear()
End Sub
11
21Windows Form programming with VB.Net 2005.
VD: Binding dữ liệu vào các TextBox
‘ Gọi phương thức Databindings.Add
Private Sub BinDingTextBox()
txtLoai.DataBindings.Add("Text", ds, "SanPham.LoaiID")
txtMa.DataBindings.Add("Text", ds, "SanPham.MaSP")
txtTen.DataBindings.Add("Text", ds, "SanPham.TenSP")
txtDVT.DataBindings.Add("Text", ds, "SanPham.DVT")
txtGia.DataBindings.Add("Text", ds, "SanPham.DonGia")
bdManager = Me.BindingContext(ds, "SanPham")
End Sub
22Windows Form programming with VB.Net 2005.
VD về di chuyển dữ liệu theo BindingManagerBase
Private Sub btnFirst_Click(sender, e) Handles btnFirst.Click
bdManager.Position = 0
End Sub
Private Sub btnPrevious_Click(sender, e) Handles btnPrevious.Click
bdManager.Position -= 1
End Sub
Private Sub btnNext_Click(sender, e) Handles btnNext.Click
bdManager.Position += 1
End Sub
Private Sub btnLast_Click(sender, e) Handles btnLast.Click
bdManager.Position = bdManager.Count
End Sub
12
23Windows Form programming with VB.Net 2005.
Content Component:
¾DataRelation: chứa các mối ràng buộc trong CSDL, có
thể chứa:
* Unique Constraint or Primary Key Constraint: 
đảm bảo tính duy nhất về giá trị của một cột trong table
* Foreign Key Constraint: ràng buộc khóa ngoại
24Windows Form programming with VB.Net 2005.
DataRelation & DataSet 
™DataRelation: cho phép thiết lập mối quan hệ
ràng buộc giữa 2 đối tượng DataTable. Việc tạo 
quan hệ này phải thỏa mãn điều kiện sau:
¾ Cột của bảng Cha (một) có quan hệ với bảng Con 
(nhiều) phải là khóa chính (Primary Key) hoặc phải 
thỏa mãn yêu cầu về tính duy nhất (Unique)
¾ Chỉ có thể thiết lập mỗi quan hệ giữa 2 bảng trong 
cùng một DataSet
13
25Windows Form programming with VB.Net 2005.
DataRelation & DataSet 
¾ Cú pháp khai báo DataRelation: 
Dim BienQH as new DataRelation (“Tên”,CộtCha, CộtCon)
Trong đó:
• Tên: là tên Bảng quan hệ được đặt tùy ý theo lập trình viên
• CộtCha: Là một DataColum có cột quan hệ với Table Con
• CộtCon: Là một DataColum có cột quan hệ với Table Cha
¾ Cú pháp đưa DataRelation vào DataSet: 
‘ Giả dử ds là biến DataSet đã được khai báo 
ds. Relations.Add(BienQH)
26Windows Form programming with VB.Net 2005.
Private Sub TaoQuanHe()
Dim daCha As New OleDbDataAdapter("select * From 
Khoa”, cn)
Dim daCon As New OleDbDataAdapter("select * From 
SinhVien”, cn)
daCha.Fill(ds, “Khoa")
daCon.Fill(ds, “SinhVien")
Dim CotCha, CotCon As DataColumn
CotCha = ds.Tables(“Khoa").Columns(“MaKH")
CotCon = ds.Tables(“SinhVien").Columns(“MaKH")
Dim QHe As new DataRelation(“KhoaSV",CotCha,CotCon)
ds.Relations.Add(QHe)
End Sub
Giả sử biến ds biến dataset, cn biến kết nối CSDL đã được khai báo
14
27Windows Form programming with VB.Net 2005.
28Windows Form programming with VB.Net 2005.
Xem Code
15
29Windows Form programming with VB.Net 2005.
Buổi 7b: Kiểm soát nhập liệu
Mục đích của 2 Event trên là để kiểm soát 
việc nhập dữ liệu đúng đắn theo tiêu chí của 
người lập trình, thường dùng cho TextBox
kết hợp với phương thức Focus và các Event 
như: Validting, LostFocus, TextChanged, 
KeyPress và phương thức Focus: chuyển 
cursor về đúng vị trí của TextBox cần nhập.
¾Focus: Khi được gọi Cursor sẽ tự động 
nhảy về vị trí của TextBox đó.
Cú pháp: tênTextBox.Focus
30Windows Form programming with VB.Net 2005.
Kiểm soát nhập liệu
¾LostFocus: Event tự động phát sinh khi 
cursor được di chuyển sang control khác
¾KeyPress: Event tự động phát sinh khi 
chúng ta gõ phím bất kỳ vào TextBox 
¾TextChanged: Event tự động phát sinh khi 
chúng ta giá trị trong TextBox được thay đổi
¾Validting: tương tự như LostFocus, nhưng 
sẽ phát sinh ra một Icon đi kèm với thông báo 
lỗi được kết hợp với đối tượng ErrorProvider
16
31Windows Form programming with VB.Net 2005.
Ví dụ: về Event LostFocus và Method Focus
Private Sub txtMa_LostFocus(sender, e) Handles txtMa.LostFocus
If txtMa.Text = "" Then
MessageBox.Show("Dữ liệu không được rỗng", "Cảng báo", 
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtMa.Focus()
End If
End Sub
32Windows Form programming with VB.Net 2005.
Ví dụ: về Event TextChanged
Private Sub TextBoxName_TextChanged(..) Handles 
txtGia.TextChanged, txtDVT.TextChanged, txtMa.TextChanged, 
txtTen.TextChanged
Dim KiemSoatNhap As Boolean
KiemSoatNhap = txtGia.Text = "" Or txtDVT.Text = "" Or
txtMa.Text = "" Or txtTen.Text = ""
If KiemSoatNhap = True Then
btnLuu.Enabled = False
Else
btnLuu.Enabled = True
End If
End Sub
17
33Windows Form programming with VB.Net 2005.
Private Sub TextBoxName_TextChanged(ByVal sender As Object, ByVal 
e As System.EventArgs) Handles txtGia.TextChanged, 
txtDVT.TextChanged, txtMa.TextChanged, txtTen.TextChanged
Dim KiemSoatNhap As Boolean = False
Dim ctrl As Control
For Each ctrl In Me.GroupBox1.Controls
If (TypeOf ctrl Is TextBox) Then
If CType(ctrl, TextBox).Text = "" Then
KiemSoatNhap = True
Exit For
End If
End If
Next
If KiemSoatNhap = True Then
btnLuu.Enabled = False
Else
btnLuu.Enabled = True
End If
End Sub
34Windows Form programming with VB.Net 2005.
Ví dụ: về Event KeyPress
Private Sub txtGia_KeyPress (sender, e) Handles txtGia. KeyPress
Dim c As Char ' khai báo biến c là kiểu ký tự
c = e.KeyChar
' Char.IsDigit(c) chỉ nhận ký tự từ 0-9; Chr(8) ký tự xóa trái
If Not Char.IsDigit(c) And c Chr(8) Then
' e.Handled Remove the character – không hiện ký tự
e.Handled = True 
MsgBox(“Phải nhập số nguyên > 0")
End If
End Sub
18
35Windows Form programming with VB.Net 2005.
Ví dụ: về Event KeyPress
Private Sub txtGia_KeyPress (sender, e) Handles txtGia. KeyPress
Dim c As Char ' khai báo biến c là kiểu ký tự
c = e.KeyChar
' chỉ nhận ký tự từ 0-9 và ký tự xóa trái là phím BackSpace
If ( c Chr(57) ) And c Chr(8) Then
e.Handled = True ' Remove the character
ErrorProvider1.SetError(sender, "Nhập số nguyên >0")
Else
ErrorProvider1.SetError(sender, "") ' Reset no Error
End If
End Sub
' Trong VD này dùng ErrorProvider1.SetError để cảnh báo thay vì
dùng MessageBox
36Windows Form programming with VB.Net 2005.
Bảng Char các phím
19
37Windows Form programming with VB.Net 2005.
Đối tượng ErrorProvider
ErrorProvider: là đối tượng dùng để cảnh báo lỗi có 2 phương thức 
thường dùng là SetError và GetError
¾ SetError: cho hiển thị thông báo lỗi và một Icon xuất hiện ngay 
cạnh control
Cú pháp: TênErrorProvider. SetError(Control, “Thông báo”)
VD: ErrorProvider1.SetError(sender, "Nhập số nguyên >0")
VD: ErrorProvider1.SetError(sender, "") ' Reset no Error
¾GetError: Lấy giá trị chuỗi thông báo trong SetError
Cú pháp: TênErrorProvider. GetError(Control)
VD: ErrorProvider1.GetError(sender)
38Windows Form programming with VB.Net 2005.
20
39Windows Form programming with VB.Net 2005.
Ví dụ: về Event Validting
Private Sub txtMa_Validating(ByVal sender As Object, ByVal e 
As System.ComponentModel.CancelEventArgs) Handles
txtMa.Validating 
'If CType(sender, TextBox).Text.Trim = "" Then
If sender.Text.Trim = "" Then
ErrorProvider1.SetError(sender, “Không được rỗng")
e.Cancel = True
Else
ErrorProvider1.SetError(sender, "") ' Reset no Error
End If
End Sub
Ghi chú: cú pháp e.Cancel = True ở trên sẽ khóa toàn bộ chức năng 
của chương trình (ngoại trừ Application.Exit) cho tới khi nhập 
đúng dữ liệu theo yêu cầu của lập trình lúc đó chương trình mới 
họa động tiếp tục
40Windows Form programming with VB.Net 2005.
Ví dụ: về Event Validting – tổng quát
Private Sub txtName_Validating(ByVal sender As Object, ByVal e 
As System.ComponentModel.CancelEventArgs) Handles
txtMa.Validating, txtDVT.Validating, txtTen.Validating, 
txtGia.Validating
If CType(sender, TextBox).Text.Trim = "" Then
ErrorProvider1.SetError(sender, “Không được rỗng")
e.Cancel = True
Else
ErrorProvider1.SetError(sender, "") ' Reset no Error
End If
End Sub

File đính kèm:

  • pdfBài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 7_VB.NET DataBinDing, kiểm soát nhập liệu.pdf