Professional Documents
Culture Documents
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:
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>
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.
5
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
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.
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
VD:
Private Sub ComboboxKhachHang()
' Khai bao Tao doi tuong trong Class clsData tai file clsData.vb
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
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
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
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
27
Xem Code
28
14
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
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
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
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, 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
20