CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Data Grid

  1. #1
    Join Date
    Apr 2001
    Posts
    29

    Data Grid

    I need a data grid that will allow entering data, valadation of a row, coloum, and edit data in cells..

    Help!!

    Thanks



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Data Grid

    You can use MSFlexGrid. Create a text box and/or combo box on a form and this code will position the text box over a cell and will give you possibility to edit the cell

    this is a sample project

    'position a combo or Text box over a cell and type there
    ==========================================
    Option Explicit

    Private Sub EndEdit()
    '--------------------------
    If Combo1.Visible Then
    MSFlexGrid1.Text = Combo1.Text
    Combo1.Visible = False
    ElseIf Text1.Visible Then
    MSFlexGrid1.Text = Text1.Text
    Text1.Visible = False
    End If
    End Sub

    Private Sub StartEdit()
    '--------------------------
    If MSFlexGrid1.Col = 2 Then
    Combo1.Clear
    Combo1.AddItem "Paranoia City"
    Combo1.AddItem "Langonsville"
    Combo1.AddItem "Codertown"
    Combo1.AddItem "Bugvanna"
    Combo1.AddItem "Abend"
    On Error Resume Next
    Combo1.Text = MSFlexGrid1.Text
    On Error GoTo 0

    GridEditCombo
    ElseIf MSFlexGrid1.Col = 3 Then
    Combo1.Clear
    Combo1.AddItem "CA"
    Combo1.AddItem "VT"
    Combo1.AddItem "MN"
    Combo1.AddItem "WY"
    Combo1.AddItem "TX"
    On Error Resume Next
    Combo1.Text = MSFlexGrid1.Text
    On Error GoTo 0

    GridEditCombo
    Else
    GridEditText Asc(" ")
    End If
    End Sub

    ' Make the change.
    Private Sub Combo1_Click()
    '--------------------------
    MSFlexGrid1.SetFocus
    End Sub


    Private Sub Form_Load()
    '--------------------------
    Dim r As Integer
    Dim c As Integer
    Dim max_len As Single
    Dim new_len As Single

    ' Use no border.
    Text1.BorderStyle = vbBSNone

    ' Make the grid's cell height match
    ' that of the ComboBox.
    MSFlexGrid1.RowHeightMin = Combo1.Height

    ' Match the grid's font.
    Text1.FontName = MSFlexGrid1.FontName
    Text1.FontSize = MSFlexGrid1.FontSize
    Text1.Visible = False
    Combo1.FontName = MSFlexGrid1.FontName
    Combo1.FontSize = MSFlexGrid1.FontSize
    Combo1.Visible = False

    ' Create some data.
    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Cols = 5
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.Rows = 6

    MSFlexGrid1.TextMatrix(0, 0) = "Name"
    MSFlexGrid1.TextMatrix(0, 1) = "Street"
    MSFlexGrid1.TextMatrix(0, 2) = "City"
    MSFlexGrid1.TextMatrix(0, 3) = "State"
    MSFlexGrid1.TextMatrix(0, 4) = "Zip"

    MSFlexGrid1.TextMatrix(1, 0) = "Andy Avaricious"
    MSFlexGrid1.TextMatrix(1, 1) = "827 Problem Blvd"
    MSFlexGrid1.TextMatrix(1, 2) = "Paranoia City"
    MSFlexGrid1.TextMatrix(1, 3) = "CA"
    MSFlexGrid1.TextMatrix(1, 4) = "98765"

    MSFlexGrid1.TextMatrix(2, 0) = "Betty Boisterous"
    MSFlexGrid1.TextMatrix(2, 1) = "4352 Main St Apt 254"
    MSFlexGrid1.TextMatrix(2, 2) = "Langonsville"
    MSFlexGrid1.TextMatrix(2, 3) = "VT"
    MSFlexGrid1.TextMatrix(2, 4) = "01234"

    MSFlexGrid1.TextMatrix(3, 0) = "Charles Cheerful"
    MSFlexGrid1.TextMatrix(3, 1) = "89 North A Ln"
    MSFlexGrid1.TextMatrix(3, 2) = "Codertown"
    MSFlexGrid1.TextMatrix(3, 3) = "MN"
    MSFlexGrid1.TextMatrix(3, 4) = "54583"

    MSFlexGrid1.TextMatrix(4, 0) = "Denise Delightful"
    MSFlexGrid1.TextMatrix(4, 1) = "89 75th"
    MSFlexGrid1.TextMatrix(4, 2) = "Bugvanna"
    MSFlexGrid1.TextMatrix(4, 3) = "WY"
    MSFlexGrid1.TextMatrix(4, 4) = "82738"

    MSFlexGrid1.TextMatrix(5, 0) = "Ed Enlightened"
    MSFlexGrid1.TextMatrix(5, 1) = "2765 N South St E"
    MSFlexGrid1.TextMatrix(5, 2) = "Abend"
    MSFlexGrid1.TextMatrix(5, 3) = "TX"
    MSFlexGrid1.TextMatrix(5, 4) = "67583"

    ' Size the columns.
    Font.Name = MSFlexGrid1.Font.Name
    Font.Size = MSFlexGrid1.Font.Size
    For c = 0 To MSFlexGrid1.Cols - 1
    max_len = 0
    For r = 0 To MSFlexGrid1.Rows - 1
    new_len = TextWidth(MSFlexGrid1.TextMatrix(r, c))
    If max_len < new_len Then max_len = new_len
    Next r
    MSFlexGrid1.ColWidth(c) = max_len + 240
    MSFlexGrid1.ColAlignment(c) = flexAlignLeftCenter
    Next c

    MSFlexGrid1.AllowUserResizing = flexResizeBoth
    End Sub

    Private Sub GridEditText(ByVal KeyAscii As Integer)
    '--------------------------
    ' Position the TextBox over the cell.
    Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Text1.Width = MSFlexGrid1.CellWidth
    Text1.Height = MSFlexGrid1.CellHeight
    Text1.Visible = True
    Text1.SetFocus

    Select Case KeyAscii
    Case 0 To Asc(" ")
    Text1.Text = MSFlexGrid1.Text
    Text1.SelStart = Len(Text1.Text)
    Case Else
    Text1.Text = Chr$(KeyAscii)
    Text1.SelStart = 1
    End Select
    End Sub

    Private Sub GridEditCombo()
    '--------------------------
    ' Position the ComboBox over the cell.
    Combo1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Combo1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Combo1.Width = MSFlexGrid1.CellWidth
    Combo1.Visible = True
    Combo1.SetFocus
    End Sub
    Private Sub Form_Resize()
    MSFlexGrid1.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub


    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    '--------------------------
    Select Case KeyCode
    Case vbKeyEscape
    ' Leave the text unchanged.
    Text1.Visible = False
    MSFlexGrid1.SetFocus

    Case vbKeyReturn
    ' Finish editing.
    MSFlexGrid1.SetFocus

    Case vbKeyDown
    ' Move down 1 row.
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
    MSFlexGrid1.Row = MSFlexGrid1.Row + 1
    End If

    Case vbKeyUp
    ' Move up 1 row.
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
    MSFlexGrid1.Row = MSFlexGrid1.Row - 1
    End If

    End Select
    End Sub
    ' Do not beep on Return or Escape.

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    '--------------------------
    If (KeyAscii = vbKeyReturn) Or _
    (KeyAscii = vbKeyEscape) _
    Then KeyAscii = 0
    End Sub

    Private Sub MSFlexGrid1_DblClick()
    '--------------------------
    StartEdit
    End Sub

    Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    '--------------------------
    If MSFlexGrid1.Col <> 2 And _
    MSFlexGrid1.Col <> 3 _
    Then
    GridEditText KeyAscii
    End If
    End Sub

    Private Sub MSFlexGrid1_LeaveCell()
    '--------------------------
    EndEdit
    End Sub

    Private Sub MSFlexGrid1_GotFocus()
    '--------------------------
    EndEdit
    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Feb 2001
    Posts
    54

    Re: Data Grid

    Hello:

    You can use this code to do it with ADO and Datagrid

    Private Sub Form_Load()
    Adodc1.Recordset.Update
    Adodc1.Recordset.Requery
    Adodc1.Recordset.AddNew
    End Sub


    Good Luck



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured