Professional Documents
Culture Documents
2005B7 PDF
2005B7 PDF
2005B7 PDF
Net DataBinDing
Mc tiu ca bi: S dng c cc DataTable,
DataView, DataSet, DataRelation, kt hp vi
BindingSource, BingdingNavigator... :
Qun l v Lin kt d liu vo cc Control c
thuc tnh (Properties) Dadabinding thng dng
nh:
9DataGridView,
9TextBox, Combobox
9DateTimePicker
9RadioButton
9CheckBox
Windows Form programming with VB.Net 2005.
BindingSource
BindingSource: gi ngun d liu cho vic qun l v
hin th d diu, thng c ly t mt DataSet;
i tng c sn trong phn thit k thuc phn Data ca
ToolBox hoc c to to i tng bng Code:
Dim bdsTn as New BindingSource()
Properties:
DataSource: ly ngun d liu t DataSet
DataMember: tn bng c Fill trong DataSet
Filter: Lc d liu t ngun d liu c ly, iu
kin lc ging i tng DataView bi trc
C php:
bdsTn.DataSource = <Bin DataSet>
bdsTn.DataMember = <Tn table>
Windows Form programming with VB.Net 2005.
BindingNavigator
BindingNavigator l i tng c sn trong phn
thit k thuc phn Data ca ToolBox;
BindingNavigator Gip qun l vic di chuyn
cc dng d liu v First, Next, Last, Previous
Properties:
BindingSource: ly ngun d liu t bin BindingSource
C php:
TnbdNavigate.BindingSource = <bin BindingSource>
VD: BindingNavigator
Binding to DataGridView
C php ly d liu t DataTable:
dgvTn. DataSource = <Bin DataTable>
C php ly d liu t DataView:
dgvTn. DataSource = <Bin DataView>
C php ly d liu t DataSet:
dgvTn. DataSource = <Bin DataSet>
dgvTn. DataMember = <Tn Bng>
C php ly d liu t BinDingSource:
dgvTn. DataSource = <Bin BidingSource>
Windows Form programming with VB.Net 2005.
Column(i).DefaultCellStyle.Alignment:nh dng
d liu trong ct l Left, Right, Center
Rows(i).Cell(j).Value: Get or Set gi tr cho
hng i, ct j
Rows(i).Selected=True/False chn/khng chn
hng i
MultiSelect = True/False cho php chn hay
khng cho php chn nhiu dng
Windows Form programming with VB.Net 2005.
10
11
12
VD:
Private Sub ComboboxKhachHang()
' Khai bao Tao doi tuong trong Class clsData tai file clsData.vb
cboKhachHang.DataSource =
objData.GetDataTable("sp_KhachHang")
cboKhachHang.DisplayMember = "TenCty"
cboKhachHang.ValueMember = "MaKH"
End Sub
Windows Form programming with VB.Net 2005.
13
Binding to Textbox
C php:
controlTn.DataBindings.Add(Text,ds, Bang.Cot)
Trong :
controlTn: Tn TextBox
ds: DataSet c fill t DataAdapter trc
Bang.Cot:
9Bang l tn table c Fill vo DataSet t
DataAdapter
9Cot l tn ct (Column) thuc table Bang
Windows Form programming with VB.Net 2005.
14
Binding to DateTimePicker
C php:
controlTn.DataBindings.Add(Value,ds, Bang.Cot)
Trong :
controlTn: Tn DateTimePicker
ds: DataSet c fill t DataAdapter trc
Bang.Cot:
9 Bang l tn table c Fill vo DataSet t
DataAdapter
9 Cot l tn ct (Column) thuc table Bang
Windows Form programming with VB.Net 2005.
15
16
BindingManagerBase
BindingManager: qun l tt c cc i tng c
gn kt (Binding) d liu trn cng mt DataSoure
v
DataMember
thng
qua
i
tng
BindingContext
Khai bo:
Dim TnBin As BindingManagerBase
TnBin = Me.BindingContext(ds, Table)
Trong :
* ds: l 1 Dataset c fill t DataAdapter
* Table: l tn bng c fill trong bin ds
Windows Form programming with VB.Net 2005.
17
18
19
20
10
21
22
11
Content Component:
DataRelation: cha cc mi rng buc trong CSDL, c
th cha:
* Unique Constraint or Primary Key Constraint:
m bo tnh duy nht v gi tr ca mt ct trong table
* Foreign Key Constraint: rng buc kha ngoi
23
24
12
ds. Relations.Add(BienQH)
25
26
13
27
Xem Code
28
14
29
30
15
31
V d: v Event TextChanged
Private Sub TextBoxName_TextChanged(
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
Windows Form programming with VB.Net 2005.
32
16
33
V d: v Event KeyPress
Private Sub txtGia_KeyPress (sender, e) Handles txtGia. KeyPress
Dim c As Char ' khai b
bo bi
bin c l
l ki
kiu k t
t
c = e.KeyChar
' Char.IsDigit(c) ch
ch nh
nhn k t
t t 0-9; Chr(8) k t
t xa tr
tri
If Not Char.IsDigit(c)
Char.IsDigit(c) And c <> Chr(8) Then
' e.Handled Remove the character khng hi
hin k t
t
e.Handled = True
MsgBox(
MsgBox(Ph
Phi nh
nhp s
s nguyn > 0")
End If
End Sub
Windows Form programming with VB.Net 2005.
34
17
V d: v Event KeyPress
Private Sub txtGia_KeyPress (sender, e) Handles txtGia. KeyPress
Dim c As Char ' khai b
bo bi
bin c l
l ki
kiu k t
t
c = e.KeyChar
' ch
ch nh
nhn k t
t t 0-9 v
v k t
t xa tr
tri l
l ph
phm BackSpace
If ( c < Chr(48) Or c > Chr(57) ) And c <> Chr(8) Then
e.Handled = True
' Remove the character
ErrorProvider1.SetError(sender,
ErrorProvider1.SetError(sender, "Nh
"Nhp s
s nguyn >0")
Else
ErrorProvider1.SetError(sender,
ErrorProvider1.SetError(sender, "") ' Reset no Error
End If
End Sub
' Trong VD n
ny d
dng ErrorProvider1.SetError
ErrorProvider1.SetError
cnh b
bo thay v
v
dng MessageBox
Windows Form programming with VB.Net 2005.
35
36
18
i tng ErrorProvider
ErrorProvider: l
i t
tng d
dng
cnh b
bo l
li c
c 2 phng th
thc
th
thng d
dng l
l SetError v GetError
SetError:
SetError: cho hi
hin th
th thng b
bo l
li v
v mt Icon xu
xut hi
hin ngay
cnh control
C ph
php: TnErrorProvider. SetError(Control, Thng b
bo)
VD: ErrorProvider1.SetError(sender,
ErrorProvider1.SetError(sender, "Nh
"Nhp s
s nguyn >0")
VD: ErrorProvider1.SetError(sender,
ErrorProvider1.SetError(sender, "")
GetError:
GetError: Ly gi
gi tr
tr chu
chui thng b
bo trong SetError
C ph
php: TnErrorProvider. GetError(Control)
VD: ErrorProvider1.GetError(sender)
ErrorProvider1.GetError(sender)
37
38
19
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,
ErrorProvider1.SetError(sender, Khng
c r
rng")
e.Cancel = True
Else
ErrorProvider1.SetError(sender,
ErrorProvider1.SetError(sender, "") ' Reset no Error
End If
End Sub
Ghi ch
php e.Cancel = True trn s kh
kha to
ton b
b ch
chc nng
ch: c ph
ca chng tr
trnh (ngo
(ngoi tr
tr Application.Exit) cho t
ti khi nh
nhp
ng d
d li
liu theo yu c
cu c
ca l
lp tr
trnh l
lc
chng tr
trnh m
mi
ha
ng ti
tip t
tc
Windows Form programming with VB.Net 2005.
39
40
20