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:

DataGridView, TextBox, Combobox DateTimePicker RadioButton CheckBox


Windows Form programming with VB.Net 2005.
1

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.
2

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.
5

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

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.
9

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: Bang l tn table c Fill vo DataSet t DataAdapter Cot 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: Bang l tn table c Fill vo DataSet t DataAdapter 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 d ds l bin DataSet c khai bo Gi l bi b

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, e) Handles txtMa.LostFocus txtMa_LostFocus(sender, If txtMa.Text = "" Then MessageBox.Show("D liu khng c rng", "Cng bo", MessageBox.Show("D li r "C b 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(..) Handles TextBoxName_TextChanged( ..) 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(ByVal sender As Object, ByVal TextBoxName_TextChanged( Object, 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 bo bin c l kiu k t b bi l ki t c = e.KeyChar ' Char.IsDigit(c) ch nhn k t t 0-9; Chr(8) k t xa tri ch nh t t tr If Not Char.IsDigit(c) And c <> Chr(8) Then Char.IsDigit(c) ' e.Handled Remove the character khng hin k t hi t e.Handled = True MsgBox(Phi nhp s nguyn > 0") MsgBox( Ph nh s 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 bo bin c l kiu k t b bi l ki t c = e.KeyChar ' ch nhn k t t 0-9 v k t xa tri l phm BackSpace ch nh t v t tr l ph If ( c < Chr(48) Or c > Chr(57) ) And c <> Chr(8) Then e.Handled = True ' Remove the character ErrorProvider1.SetError(sender, "Nhp s nguyn >0") ErrorProvider1.SetError(sender, "Nh s Else ErrorProvider1.SetError(sender, "") ' Reset no Error ErrorProvider1.SetError(sender, End If End Sub ' Trong VD ny dng ErrorProvider1.SetError cnh bo thay v n d ErrorProvider1.SetError b 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 tng dng cnh bo li c 2 phng thc t d b l c th thng dng l SetError v GetError th d l SetError: cho hin th thng bo li v mt Icon xut hin ngay SetError: hi th b l v xu hi cnh control C php: TnErrorProvider. SetError(Control, Thng bo) ph b VD: ErrorProvider1.SetError(sender, "Nhp s nguyn >0") ErrorProvider1.SetError(sender, "Nh s VD: ErrorProvider1.SetError(sender, "") ErrorProvider1.SetError(sender, C php: TnErrorProvider. GetError(Control) ph VD: ErrorProvider1.GetError(sender) ErrorProvider1.GetError(sender) ' Reset no Error GetError: Ly gi tr chui thng bo trong SetError GetError: gi tr chu b

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, Khng c rng") ErrorProvider1.SetError(sender, r e.Cancel = True Else ErrorProvider1.SetError(sender, "") ' Reset no Error ErrorProvider1.SetError(sender, End If End Sub Ghi ch: c php e.Cancel = True trn s kha ton b chc nng ph kh to b ch ch ca chng trnh (ngoi tr Application.Exit) cho ti khi nhp tr (ngo tr t nh ng d liu theo yu cu ca lp trnh lc chng trnh mi d li c c l tr l tr m ha ng tip tc ti t
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, TextBox).Text.Trim = "" Then CType(sender, ErrorProvider1.SetError(sender, Khng c rng") ErrorProvider1.SetError(sender, r e.Cancel = True Else ErrorProvider1.SetError(sender, "") ' Reset no Error ErrorProvider1.SetError(sender, End If End Sub
Windows Form programming with VB.Net 2005.
40

20

You might also like