Global It Leader!!



 
 

VB DBGrid 응용예제

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 4,389회 작성일 16-04-27 11:05

본문

다음 예제에서는 네 개의 필드 및 다섯 개의 레코드가 한 테이블에 들어 있는 간단한 데이터베이스를 만듭니다. 각 레코드에는 필드에 숫자입니다. 데이터베이스는 데이터 원본이 될 것입니다.

셀을 클릭할 때 해당 셀의 데이터가 TextBox에 표시 됩니다. 해당 셀의 내용을 변경 하면, 변경 내용은 DBGrid 컨트롤에 표시 됩니다. 그러나 명령 단추를 통해 UpdateControls 메서드를 실행 해도 변경 데이터 소스가 업데이트 되지 않았습니다 나타내는 셀에서 사라집니다.

변경 내용을 데이터 소스에 기록 하는 확인 텍스트 상자에 변경할 때 업데이트 Recordset 단추를 클릭 해야 합니다. 데이터 컨트롤의 기본 레코드 집합이 업데이트 Recordset 단추를 업데이트합니다. DBGrid 컨트롤이 데이터 컨트롤에 바인딩되어 있으므로 DBGrid 컨트롤에 나타납니다.

샘플 응용 프로그램 만들기

다음 샘플 응용 프로그램에서는 DBGrid 셀의 내용을 편집 하는 방법을 보여 줍니다.
  1. Visual Basic 시작 합니다. 파일 메뉴에서 실행 되 면 새 프로젝트를 클릭 합니다. 데이터 컨트롤, DBGrid 컨트롤, 텍스트 상자, 및 세 개의 명령 단추를 추가 합니다. 각 컨트롤에 대해 다음 속성을 설정 합니다.
       Control       Default Name       Property       Value
       -----------------------------------------------------
    
       DBGrid        DBGrid1            DataSource     Data1
    
    						
  2. Form1 코드 창에 다음 코드 샘플을 복사 합니다.
          Option Explicit
          Dim rs1 As Recordset
          Dim db As Database
          Dim td As TableDef
          Dim fl As Field
          Dim igRow As Integer, igColumn As Integer
          Dim iFields As Integer, iRecords As Integer
          Dim vargBookmark As Variant
    
          Private Sub Command1_Click()
             ' The Create Database button: By clicking this button, you create
             ' a database with four fields and five records.
    
             Set db = CreateDatabase("C:\test.mdb", dbLangGeneral)
             Set td = db.CreateTableDef("Table1")
    
             ' After you create the database, you need to add fields to it.
             For iFields = 1 To 4 ' The last number can be changed to the
                                  ' number of fields you want in the database.
                Set fl = td.CreateField("Field " & CStr(iFields), dbText)
                td.Fields.Append fl
             Next iFields
    
             db.TableDefs.Append td
    
             ' Now that you have added fields to the database, you need to add
             ' some records through a recordset.
             Set rs1 = db.OpenRecordset("Table1", dbOpenTable)
             For iRecords = 1 To 5  'For each row
                rs1.AddNew          'Add a new record
    
                For iFields = 1 To 4        ' For each field in the record, add
                   rs1("Field " & CStr(iFields)) = CStr(iFields) ' a number.
                Next iFields
    
             rs1.Update
             Next iRecords
    
             ' Close both the recordset and database.
             rs1.Close
             db.Close
    
             ' Populate the DBGrid control with the contents of the Recordset.
             Set db = OpenDatabase("C:\test.mdb")
             Set rs1 = db.OpenRecordset("Select * from Table1")
             Set Data1.Recordset = rs1
    
             Command1.Visible = False
             Command2.Visible = True
             Command4.Visible = True
          End Sub
    
          Private Sub Command2_Click()
             ' The Update Database button: By clicking this button, you save
             ' the contents of the text box to the database. Since the contents
             ' of the recordset are being modified, the contents are saved to
             ' the database after you execute the Update method.
    
             Data1.Recordset.Edit
             Data1.Recordset.Fields(igColumn) = Text1.Text
             Data1.Recordset.Update
          End Sub
    
          Private Sub Command3_Click()
             ' The Update DBGrid button: By clicking this button, you execute
             ' the UpdateControls method on the Data control to demonstrate
             ' that changing the cell in a bound DBGrid control does not save
             ' the new information to the database. To save these changes, you
             ' must modify the underlying recordset from the Data control.
    
             Data1.UpdateControls
          End Sub
    
          Private Sub Command4_Click()
             ' The Add New Record button: By clicking this button, you add new
             ' records to the recordset. Use the following code to add a new
             ' record to the DBGrid control.
    
             ' Set DBGrid and Data Control Properties to allow new records to
             ' be added.
             DBGrid1.AllowAddNew = True
             Data1.EOFAction = vbAddNew
             Data1.Recordset.MoveLast
             Data1.Recordset.MoveNext
             DBGrid1.Row = DBGrid1.VisibleRows - 1
             Data1.Recordset.AddNew
             For iFields = 1 To 4    ' For each field in the record,
                                     ' add the contents of the text box.
                Data1.Recordset("Field " & CStr(iFields)) = Text1.Text
             Next iFields
             Data1.Recordset.Update
    
          End Sub
    
          Private Sub DBGrid1_Change()
             Command3.Visible = True
          End Sub
    
          Private Sub DBGrid1_MouseUp(Button As Integer, Shift As Integer, _
                                      X As Single, Y As Single)
             Command2.Visible = True
             igColumn = DBGrid1.ColContaining(X)
             igRow = DBGrid1.RowContaining(Y)
             vargBookmark = DBGrid1.RowBookmark(igRow)
    
             Text1.Text = DBGrid1.Columns(igColumn).CellValue(vargBookmark)
    
          End Sub
    
          Private Sub Form_Load()
             Command1.Visible = False
             Command2.Visible = False
             Command3.Visible = False
             Command4.Visible = False
             Command1.Caption = "Create Database"
             Command2.Caption = "Update Database"
             Command3.Caption = "Update DBGrid"
             Command4.Caption = "Add New Record"
    
             ' If the database does not exist, show the Create Database button.
             If Dir("C:\test.mdb") = "" Then
                Command1.Visible = True
             Else
                ' Open an existing database.
                Set db = OpenDatabase("C:\test.mdb")
                Set rs1 = db.OpenRecordset("Select * from Table1")
                Set Data1.Recordset = rs1
                Command4.Visible = True
             End If
    
          End Sub
    
          Private Sub Text1_Change()
             Command2.Visible = True
          End Sub
    
    						

샘플 응용 프로그램을 실행합니다.

  1. 실행 메뉴에서 시작을 클릭 하거나 F5 키를 누릅니다.
  2. DBGrid 컨트롤의 셀을 클릭 하 고 해당 셀의 내용을 변경 합니다. DBGrid 업데이트 단추를 클릭 합니다. 참고 변경 내용을 DBGrid에서 사라질 것입니다. DBGrid 컨트롤의 빈 영역을 클릭 하면 오류 6148-잘못 된 행 번호를 제공 합니다.
  3. 셀을 클릭 합니다. 셀의 내용을 텍스트 상자에 나타나는 참고. TextBox의 내용을 변경한 다음 업데이트 Recordset 단추를 클릭 합니다. DBGrid 컨트롤의 변경 내용을 표시 하는 참고.
  4. DBGrid 컨트롤에 새 레코드를 추가 하려면 새 레코드 추가 단추를 클릭 합니다. 텍스트 상자의 내용이 새 레코드로 추가 됩니다.

댓글목록

등록된 댓글이 없습니다.

전체 440
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
360 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4992 09-29
359 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3955 09-29
358 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4086 09-28
357 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4880 09-02
356 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7687 08-09
355 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 12604 07-11
354 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7597 06-25
353 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4107 06-24
352 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4295 06-21
351 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4131 06-16
350 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4538 06-07
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4390 04-27
348 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8758 04-08
347 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4208 04-07
346 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6042 04-07
345 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6326 03-28
344 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4163 02-16
343 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4092 12-25
342 CSS no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3966 12-25
341 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4610 12-22