2005B7 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Bui 7a: VB.

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>

Windows Form programming with VB.Net 2005.

VD: BindingNavigator

Windows Form programming with VB.Net 2005.

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.

Windows Form programming with VB.Net 2005.

Thuc tnh v phng thc (DataGridView - dgv)

Column: tp hp (Collection) cc thnh phn d


liu lin quan n cc ct ca DataGridView
Column(i).Name: tn ca ct th i
Column(i).DataPropertyname: tn ct th i tng
ng vi tn ct ca table trong CSDL
Column(i).Width: quy nh chiu rng cho ct i
Column(i).HeaderText: nh li tiu ct cho i
Column(i).ColumnType: quy nh ct th i l mt
Textbox, CheckBox, Combobox, Button, Image hoc
link
Windows Form programming with VB.Net 2005.

Thuc tnh v phng thc (DataGridView - dgv)

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.

S kin (Event of DataGridView - dgv)


CellClick(sender,e): Thc thi khi click vo mt Cell
() bt k trn dgv
CellEnter(sender,e): Thc thi khi Cell () bt k
trn dgv c chn (hay focus)
Tham s e ca 2 Event trn c 2 thuc tnh quan
trng truy xut d liu ti Cell l:
e.RowIndex: tr v th t dng ca Cell hin hnh
e.ColumnIndex: tr v th t ct ca Cell hin hnh
Kt hp thuc tnh Rows(i).Cell(j).Value vi 2
Event ny truy xut d liu ti Cell hin hnh (ct,
dng u tin c tnh t 0).
Windows Form programming with VB.Net 2005.

Private Sub TaoTieuDe()


dgvSanPham.Columns(0).HeaderText = "M"
dgvSanPham.Columns(0).Width = 40
dgvSanPham.Columns(0).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter
dgvSanPham.Columns(1).HeaderText = "Loi"
dgvSanPham.Columns(1).Width = 40
dgvSanPham.Columns(1).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter
dgvSanPham.Columns(2).HeaderText = "Tn sn phm"
dgvSanPham.Columns(2).Width = 165
dgvSanPham.Columns(3).HeaderText = "VT"
dgvSanPham.Columns(3).Width = 50
dgvSanPham.Columns(3).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter
dgvSanPham.Columns(4).HeaderText = "n gi"
dgvSanPham.Columns(4).Width = 70
dgvSanPham.Columns(4).DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight
End Sub
Windows Form programming with VB.Net 2005.

10

Binding to Combobox v ListBox


Khi Binding d liu Combobox hoc Listbox th
d liu s c hin th t ng thng qua ngun
d liu sn c ca CSDL.
C php:
controlTn.DataSource = <Bin DataSoucre>
controlTn.DisplayMember = <Tn ct hin th>
controlTn.ValueMember = <Tn ct ly d liu>
Ghi ch: <Bin DataSoucre> L ngun d liu ly
t DataTable, DataView, DataSet, hoc thng qua
BinDingSource; <Tn ct > y l ct ca
table no trong CSDL khi ta fill t DataAdapter
Windows Form programming with VB.Net 2005.

11

Binding to Combobox v ListBox (tt)


Ghi ch: Chng ta thng ly d liu ca
Combobox hoc Listbox theo thuc tnh
SelectedItem or SelectedText;
Trong trng hp ta dng Binding hin th v
ly d liu x l (vit code) t mt Combobox
hoc Listbox Chng ta phi dng thuc tnh
SelectedValue v lc d liu c ly l ct
<Tn ct ly d liu> ca thuc tnh
controlTn.ValueMember = <Tn ct ly d liu>
Trong khi :
controlTn.DisplayMember = <Tn ct hin th>
ch d nhim v hin th thng tin trn mn hnh.
Windows Form programming with VB.Net 2005.

12

VD:
Private Sub ComboboxKhachHang()
' Khai bao Tao doi tuong trong Class clsData tai file clsData.vb

Dim objData As New clsData


' Goi ham GetDataTable trong Class clsData

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

Binding to RadioButton & CheckBox


C php:
Tn.DataBindings.Add(Checked,ds, Bang.Cot)
-------------------------------------------------------- Ghi ch:
Tt c cc control c chc nng DataBindings u c
phng thc Clear xa tt c cc d liu Binding
trc (thng dng chc nng ny reset li d
liu). Trc khi dng Databindings.Add ta nn dng
phng thc Clear trc mi control cn Binding.
C php:
controlTn.DataBindings.Clear()
Windows Form programming with VB.Net 2005.

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

Thuc tnh ca BindingManagerBase


Count: Tr v s dng (Row) d liu c qun
l bi BindingManagerBase
Position: Get or Set v tr hin ti ca dng d
liu hin hnh. Ngha l khi cc TextBox,
DateTimePicker, Combobox c Binding
khi Position thay i th cc d liu hin th trn
cc i tng ny cng thay i theo. V tr u
tin c tnh t 0->count-1

Windows Form programming with VB.Net 2005.

18

Windows Form programming with VB.Net 2005.

19

VD: Binding d liu vo cc TextBox


Gi phng thc ny trc Databindings.Add
Private Sub ClearBinDingTextBox()
txtLoai.DataBindings.Clear()
txtMa.DataBindings.Clear()
txtTen.DataBindings.Clear()
txtDVT.DataBindings.Clear()
txtGia.DataBindings.Clear()
End Sub

Windows Form programming with VB.Net 2005.

20

10

VD: Binding d liu vo cc TextBox


Gi phng thc 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

Windows Form programming with VB.Net 2005.

21

VD v di chuyn d liu 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
Windows Form programming with VB.Net 2005.

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

Windows Form programming with VB.Net 2005.

23

DataRelation & DataSet


DataRelation: cho php thit lp mi quan h
rng buc gia 2 i tng DataTable. Vic to
quan h ny phi tha mn iu kin sau:
Ct ca bng Cha (mt) c quan h vi bng Con
(nhiu) phi l kha chnh (Primary Key) hoc phi
tha mn yu cu v tnh duy nht (Unique)
Ch c th thit lp mi quan h gia 2 bng trong
cng mt DataSet

Windows Form programming with VB.Net 2005.

24

12

DataRelation & DataSet


C php khai bo DataRelation:
Dim BienQH as new DataRelation (Tn,CtCha, CtCon)
Trong :
Tn: l tn Bng quan h c t ty theo lp trnh vin
CtCha: L mt DataColum c ct quan h vi Table Con
CtCon: L mt DataColum c ct quan h vi Table Cha
C php a DataRelation vo DataSet:
Gi
Gi d ds l
l bi
bin DataSet
c khai b
bo

ds. Relations.Add(BienQH)

Windows Form programming with VB.Net 2005.

25

Gi s bin ds bin dataset, cn bin kt ni CSDL c khai bo

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
Windows Form programming with VB.Net 2005.

26

13

Windows Form programming with VB.Net 2005.

27

Xem Code

Windows Form programming with VB.Net 2005.

28

14

Bui 7b: Kim sot nhp liu


Mc ch ca 2 Event trn l kim sot
vic nhp d liu ng n theo tiu ch ca
ngi lp trnh, thng dng cho TextBox
kt hp vi phng thc Focus v cc Event
nh: Validting, LostFocus, TextChanged,
KeyPress v phng thc Focus: chuyn
cursor v ng v tr ca TextBox cn nhp.
Focus: Khi c gi Cursor s t ng
nhy v v tr ca TextBox .
C php: tnTextBox.Focus
Windows Form programming with VB.Net 2005.

29

Kim sot nhp liu


LostFocus: Event t ng pht sinh khi
cursor c di chuyn sang control khc
KeyPress: Event t ng pht sinh khi
chng ta g phm bt k vo TextBox
TextChanged: Event t ng pht sinh khi
chng ta gi tr trong TextBox c thay i
Validting: tng t nh LostFocus, nhng
s pht sinh ra mt Icon i km vi thng bo
li c kt hp vi i tng ErrorProvider
Windows Form programming with VB.Net 2005.

30

15

V d: v Event LostFocus v Method Focus


Private Sub txtMa_LostFocus(sender,
txtMa_LostFocus(sender, e) Handles txtMa.LostFocus
If txtMa.Text = "" Then
MessageBox.Show("D
MessageBox.Show("D li
liu khng
c r
rng", "C
"Cng b
bo",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtMa.Focus()
End If
End Sub

Windows Form programming with VB.Net 2005.

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

Private Sub TextBoxName_TextChanged(


TextBoxName_TextChanged(ByVal sender As Object,
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
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
Windows Form programming with VB.Net 2005.

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

Bng Char cc phm

Windows Form programming with VB.Net 2005.

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, "")

' Reset no Error

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)

Windows Form programming with VB.Net 2005.

37

Windows Form programming with VB.Net 2005.

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

V d: v Event Validting tng qut


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,
CType(sender, TextBox).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
Windows Form programming with VB.Net 2005.

40

20

You might also like