Upload
nguyen-anh-dung
View
217
Download
0
Embed Size (px)
Citation preview
8/14/2019 BaiGiang6
1/12
LP TRNH C S D LIU
Bi 6:
DataGrid v DataBindings
8/14/2019 BaiGiang6
2/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 2
Ni dung
iu khin DataGrid
nh dng iu khin TextBox vo DataGrid
nh dng iu khin CheckBox vo DataGrid iu hng d liu (s dng DataBindings)
Bi tp tng hp
8/14/2019 BaiGiang6
3/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 3
ADO.NETGii thiu chung DataGrid
Hin th d liu: Ngun d liu hin th trn
DataGrid thng l Dataset, DataTable, DataView
hay t 1 file XML hin th d liu ta dng lnh:
TnDataGrid.DataSource= Tn ngun
nh dng cho DataGrid ta s dng thuc tnhTableStyles
8/14/2019 BaiGiang6
4/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 4
ADO.NETGii thiu chung DataGrid
L i tng rt hay dng trong vic hin th vnhp s liu
Mt s thuc tnh cn nh
Name AlternatingBackColor CaptionText DataSource ReadOnly TableStyle Item, CurrentCell
8/14/2019 BaiGiang6
5/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 5
ADO.NETGii thiu chung DataGrid
V d v hin th d liuTry
If mycon.State = ConnectionState.Closed Thenmycon.Open()
End IfDasv.Fill(dt) in d liu vo DataTable
Catch ex As ExceptionMsgBox(Err.Number & " " & Err.Description)
End TryWith DataGrid1 .DataSource = dtgn ngun DL cho Grid
.AllowSorting = TrueEnd With
8/14/2019 BaiGiang6
6/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 6
ADO.NETGii thiu chung DataGrid
nh dng DataGrid Thay i rng ca cc ct trong Grid Thay i mu nn ca tng hng d liu Thay i cc thuc tnh khc nh: Ch c, Tiu
ca ct, mu ca li, Thm cc i tng nh: ComboBox, TextBox,
CheckBox vo Grid S dng i tng DataGridTableStyle
thit lp nh dng cho DataGrid
8/14/2019 BaiGiang6
7/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 7
ADO.NETGii thiu chung DataGrid
V d v nh dng DataGridDim St As New DataGridTableStyle
With St
.HeaderForeColor = Color.Red
.SelectionBackColor = Color.Gray
.ReadOnly = True
.AlternatingBackColor = Color.Bisque
End With
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(St)
8/14/2019 BaiGiang6
8/12
8/14/2019 BaiGiang6
9/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 9
ADO.NETV d v TextBox DataGrid
Dim StC As New DataGridTableStyleDim dc As New DataColumnFor Each dc In dt.Columns
Dim txt As New DataGridTextBoxColumnWith txt
.MappingName = dc.ColumnNameIf dc.ColumnName = "HotenSv" Then
.Width = 120
.ReadOnly = False
.TextBox.BackColor = Color.RedEnd If.HeaderText = dc.ColumnName 'Gn tn cho cc ct
End WithStC.GridColumnStyles.Add(txt)
NextDataGrid1.TableStyles.Add(StC) DataGrid1.TableStyles.Clear()
8/14/2019 BaiGiang6
10/12
ADO.NETV d v CheckBox DataGrid
Dim StC As New DataGridTableStyleDim dc As New DataColumnFor Each dc In dt.Columns
If dc.ColumnName = "Gioitinh" Then 'Thit lp CheckboxDim txt As New DataGridBoolColumnWith txt
.MappingName = dc.ColumnName
.HeaderText = dc.ColumnName 'Gn tn cho cc ct
.ReadOnly = False
.FalseValue = 0.TrueValue = 1End WithStC.GridColumnStyles.Add(txt)
ElseDim txt1 As New DataGridTextBoxColumnWith txt
.MappingName = dc.ColumnName.ReadOnly = True
.HeaderText = dc.ColumnName 'Gn tn cho cc ctEnd WithStC.GridColumnStyles.Add(txt1)
End IfNext
DataGrid1.TableStyles.Clear()DataGrid1.TableStyles.Add(StC)
8/14/2019 BaiGiang6
11/12
8/14/2019 BaiGiang6
12/12
09/07/08 Lp trnh CSDL vi Vb.net & SQL 12
ADO.NETiu hng d liu - Navigation
VB.net cung cp i tng BindingManagerBase qun l
ngun d liu lin kt, cho php thc hin iu hng d
liu
S dng BindingManagerBase: Np ngun d liu vo i tng
Dim Bmb as BindingManagerBase khai bo i tng
Bmb=Me.BindingContext(Dt) Dt l DataTable d liu
Di chuyn cc bn ghi
Bmb.Position=0 chuyn v bn ghi u tin
Bmb.Position=Dt.Rows.Count-1 bn ghi cui cng