Lm th? no ? lin k?t m?t i?u khi?n DataGrid v?i m?t ArrayList c?a cc ?i t?ng ho?c c?u trc b?ng cch s? d?ng Visual Basic 2005 ho?c Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 316302 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i m?t Microsoft Visual C#.NET Phin b?n c?a bi vi?t ny, xem 316303.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny m t? cch ? rng bu?c m?t ArrayList cc ?i t?ng ? m?t DataGrid ki?m sot. Cc v d? bao g?m m?t h?nh th?c Microsoft Windows v?i m?t DataGrid i?u khi?n ? hi?n th? gi tr? ti s?n ?i t?ng, v b?n nt l?nh ? duy?t cc hng c?a cc DataGrid ki?m sot.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi d?ch v? ?c yu c?u:
  • Microsoft Visual Basic 2005 ho?c Microsoft Visual Basic.NET
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • Visual Basic l?p tr?nh khi ni?m

Thi?t k? cc l?p h?c

M?t l?p m l b? rng bu?c ? ki?m sot m?t ph?i c ti s?n accessors. B?t k? ti s?n m l b? rng bu?c ph?i c B?t ?ng s?n t?p v B?t ?ng s?n Get phng php. L?p m?u ?c s? d?ng trong bi vi?t ny c ba thnh vin (duy nh?t ?c hi?n th? ? y.) M?t nh xy d?ng parameterized c?ng ? ?c cung c?p, nhng khng ph?i l m?t yu c?u.
Public Class guitar
    Private m_make As String
    Private m_model As String
    Private m_year As Short

    Public Sub New(ByVal make, ByRef model, ByVal year)
        m_make = make
        m_model = model
        m_year = year
    End Sub
    Public Property make() As String
        Get
            Return m_make
        End Get
        Set(ByVal Value As String)
            m_make = Value
        End Set
    End Property
    
End Class
				

Thm l?p th? hi?n cho m?t ArrayList

? t?o ra tr?ng h?p v thm chng vo cc ArrayList, h?y lm theo cc b?c sau:
  1. Tuyn b? m?t ArrayList.
  2. T?o s? ki?n v? cc l?p h?c, v sau thm cc tr?ng h?p cho cc ArrayList.
Private al as New Arraylist()

al.Add(New guitar("Gibson", "Les Paul", 1958))
al.Add(New guitar("Fender", "Jazz Bass", 1964))
al.Add(New guitar("Guild", "Bluesbird", 1971))
				

Lin k?t ArrayList v?i DataGrid

Sau khi cc ArrayList ? ?c dn c, ?t cc DataSource ti s?n c?a cc DataGrid ki?m sot ? cc ArrayList. Cc c?t trong cc DataGrid ki?m sot ?c dn c d?a trn cc ti s?n m ti s?n trong ph?m vi accessors t?n t?i.
DataGrid1.DataSource = al
				

Cung c?p m?t phng ti?n ? tr?nh duy?t ArrayList

B?n c th? s? d?ng CurrencyManager duy?t qua cc ArrayList. ? th?c hi?n vi?c ny, k?t h?p CurrencyManager v?i cc BindingContext c?a cc i?u khi?n (trong tr?ng h?p ny, cc ArrayList).
Private cManager As CurrencyManager

cManager = CType(DataGrid1.BindingContext(al), CurrencyManager)
				
Cc CurrencyManager l?p h?c c m?t V? tr b?t ?ng s?n m b?n c th? thao tc ? iterate qua cc thnh vin c?a cc ArrayList. B?ng cch thm vo, ho?c tr? i t? gi tr? hi?n t?i c?a V? tr, b?n c th? duy?t cc hng c?a cc DataGrid ki?m sot.
'Move forward one element.
cManager.Position += 1
'Move back one element.
cManager.Position -= 1
'Move to the beginning.
cManager.Position = 0
'Move to the end.
cManager.Position = al.Count - 1
				

V d?-by-step

  1. Trong tr?c quan Basic2005 ho?c trong Visual Basic.NET, t?o ra m?t d? n ?ng d?ng Windows m?i. Form1 ?c t?o ra theo m?c ?nh.
  2. Trn cc D an tr?nh n, nh?p vo Thm Class ? thm m?t l?p h?c cho d? n.
  3. Thay th? m? trong Class1.vb v?i nh?ng i?u sau y:
    Public Class guitar
        Private m_make As String
        Private m_model As String
        Private m_year As Short
    
        Public Sub New(ByVal make, ByRef model, ByVal year)
            m_make = make
            m_model = model
            m_year = year
        End Sub
        Public Property make() As String
            Get
                Return m_make
            End Get
            Set(ByVal Value As String)
                m_make = Value
            End Set
        End Property
        Public Property model() As String
            Get
                Return m_model
            End Get
            Set(ByVal Value As String)
                m_model = Value
            End Set
        End Property
        Public Property year() As Short
            Get
                Return m_year
            End Get
            Set(ByVal Value As Short)
                m_year = Value
            End Set
        End Property
    End Class
    					
  4. ng c?a s? m? Class1.vb, v sau chuy?n sang thi?t k? m?u.
  5. Thm m?t DataGrid ki?m sot ? Form1. Kch th?c cc DataGrid ki?m sot ? ch?a b?n c?t v ba hng.
  6. Thm b?n Nt i?u khi?n ? Form1, v sau s?p x?p cc nt theo chi?u ngang.
  7. Thay ?i cc Vn b?n ti s?n c?a Button1 ? Ti?p theo.
  8. Thay ?i cc Vn b?n ti s?n c?a Button2 ? Tr?c .
  9. Thay ?i cc Vn b?n ti s?n c?a Button3 ? u tin.
  10. Thay ?i cc Vn b?n ti s?n c?a Button4 ? Cu?i cng.
  11. Thm m? sau y ? cc Form1 l?p:
    Private cManager As CurrencyManager
    Private al as New ArrayList()
    					
  12. Dn o?n m? sau trong cc Form1_Load s? ki?n:
    al.Add(New guitar("Gibson", "Les Paul", 1958))
    al.Add(New guitar("Fender", "Jazz Bass", 1964))
    al.Add(New guitar("Guild", "Bluesbird", 1971))
    				
    cManager = CType(DataGrid1.BindingContext(al), CurrencyManager)
    	
    DataGrid1.DataSource = al
    					
  13. Dn m? sau y sau khi cc Form_Load th? t?c:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            cManager.Position += 1
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            cManager.Position -= 1
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            cManager.Position = 0
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            cManager.Position = al.Count - 1
        End Sub
    					
  14. Xy d?ng v ch?y cc d? n.
  15. Nh?p vo nt l?nh ? di chuy?n gi?a cc hng c?a cc DataGrid ki?m sot. Lu ? r?ng b?n c th? ch?nh s?a cc gi tr? c?a cc ?i t?ng n?u b?n mu?n.

S? d?ng m?t c?u trc thay v? m?t l?p h?c

Quy t?c cho cc rng bu?c m?t c?u trc l tng t? nh cc quy t?c cho rng bu?c m?t ?i t?ng. B?t ?ng s?n (c ngh?a l, ti kho?n c?a) accessors ?c yu c?u. M?t c?u trc ?c t?o ra cho m?c ch ny tng t? nh m?t l?p h?c.

Lin k?t v?i m?t ArrayList k?t c?u, lm theo cc b?c sau:
  1. Thay ?i ?nh ngh?a c?a m-un h?ng Class1.vb trong v d? t?
    Public Class guitar
    '...
    End Class
    					
    ?n sau y:
    Public Structure guitar
    '...
    End Structure
    					
  2. Xy d?ng v ch?y chng tr?nh v d? m?t l?n n?a, v xc minh r?ng n ch?c nng v?i m?t ArrayList cc c?u trc.

THAM KH?O

? bi?t thm chi ti?t, xem ch? ? "Ng?i tiu dng c?a d? li?u trn Windows Forms" trong Visual Studio.NET tr? gip tr?c tuy?n.

Thu?c tnh

ID c?a bi: 316302 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
T? kha:
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB316302 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:316302

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com