Welcome / Home
Understanding My PC
Software Downloads
VB.NET
JAVA
C#.NET
RPG
SQL
Software Engineering
Networking
Communications
Security
Internet Links
Contact Us

How to Code a DataGrid


 

Most transactions are best viewed and edited in DataGrids.  Think of a DataGrid as a Spreadsheet Page.  First we formatted the GRID, then we populate the Grid and an array with an object for hidden information.  We are using a text box and Compbox to work with each cells.  We also have a routine to move to the next cell after an edit completion (Return Key pushed).  Finally we have coded a section to go through each line in the grid updating the database as needed.  We use HEADLINE Scripting with lowercase prefixes for most objects.  We also use acronyms for field names  (AccountNumber=ACTNBR).   Please pardon the formatting from the HTML editor.  Indentations were lost, and it selected some funky fonts and colors.
 
Copyrighted by CompuCranks.com (2006)
 
 
'****************************************************************************************
Private Sub prcFORMAT_XXXXXX_GRID()
'****************************************************************************************
   With grdTRANSACTION
      .ColumnCount = 4
      .Columns(0).HeaderText = "Act Nbr"
      .Columns(0).Width = 75
      .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
      .Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
      .Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
      .Columns(1).HeaderText = "Act Name"
      .Columns(1).Width = 250
      .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
      .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
      .Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
      .Columns(2).HeaderText = "Description"
      .Columns(2).Width = grdTRANSACTION.Width - 423
      .Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
      .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
      .Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
      .Columns(3).HeaderText = "$$$$"
      .Columns(3).Width = 75
      .Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
      .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
      .Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
      .AllowUserToAddRows = False
      .AllowUserToDeleteRows = False
   End With
End Sub
 
'****************************************************************************************
Private Sub prcBUILD_XXXXXX_GRID()
'****************************************************************************************
   Dim R As Integer = 0
   Call prcSQLXXT("TRANSACTIONS")
   With grdTRANSACTION
      .RowCount = 0
      .RowCount = 20
      Do While rdrXXT.Read()
         .Item(0, R).Value = CType(rdrXXT.Item("xxtACTNBR"), String)
         .Item(1, R).Value = CType(rdrXXT.Item("xxmACTNME"), String)
         .Item(2, R).Value = CType(rdrXXT.Item("xxtDESCPT"), String)
         .Item(3, R).Value = CType(Format(rdrXXT.Item("xxtTRNAMT"), "####0.00"), String)
         aryXXT.Add(New keyXXT(CLng(rdrXXT.Item("xxtACTNBR")), CInt(rdrXXT.Item("xxtTRNCNT"))))
         R = R + 1
         If R > .RowCount - 5 Then
            .RowCount = .RowCount + 10
          End If
      Loop
      .CurrentCell.Selected = False
   End With
   flgBLDGRD = True
End Sub
 
'****************************************************************************************
Private Sub grdTRANSACTION_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTRANSACTION.CellClick
'****************************************************************************************
   Call prcPOPULATE_XXXXXX_CELL()
   Call prcPOPULATE_XXXXXX_EDIT()
End Sub
 
'****************************************************************************************
Private Sub tbxTRANSACTION_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbxTRANSACTION.KeyDown
'****************************************************************************************
   If e.KeyCode = Keys.Return Then
      Call prcPOPULATE_XXXXXX_CELL()
      If flgCELVLD = True Then
          Call prcNEXT_XXXXXX_CELL()
      End If
      Call prcPOPULATE_XXXXXX_EDIT()
   End If
End Sub
 
'****************************************************************************************
Private Sub tbxTRANSACTION_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbxTRANSACTION.LostFocus
'****************************************************************************************
   tbxTRANSACTION.Visible = False
   cbxTRANSACTION.Visible = False
   flgGRDEDT = False
End Sub
 
'****************************************************************************************
Private Sub cbxTRANSACTION_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbxTRANSACTION.KeyDown
'****************************************************************************************
   If e.KeyCode = Keys.Return Then
      Call prcPOPULATE_XXXXXX_CELL()
      Call prcNEXT_XXXXXX_CELL()
      Call prcPOPULATE_XXXXXX_EDIT()
   End If
End Sub
 
'****************************************************************************************
Private Sub cbxTRANSACTION_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxTRANSACTION.LostFocus
'****************************************************************************************
   tbxTRANSACTION.Visible = False
   cbxTRANSACTION.Visible = False
   flgGRDEDT = False
End Sub
 
'****************************************************************************************
Private Sub grdTRANSACTION_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles grdTRANSACTION.Scroll
'****************************************************************************************
   tbxTRANSACTION.Visible = False
   cbxTRANSACTION.Visible = False
   flgGRDEDT = False
End Sub
 
'****************************************************************************************
Private Sub prcPOPULATE_XXXXXX_EDIT()
'****************************************************************************************
   If flgBLDGRD = True And flgGRDEDT = False Then
      Select Case grdTRANSACTION.CurrentCellAddress.X
      Case 1
         cbxTRANSACTION.Size = grdTRANSACTION.CurrentCell.Size
         cbxTRANSACTION.Location = CellLocation(grdTRANSACTION, grdTRANSACTION.CurrentCellAddress.X, grdTRANSACTION.CurrentCellAddress.Y)
         Try
            cbxTRANSACTION.Text = grdTRANSACTION.CurrentCell.Value.ToString
         Catch
           cbxTRANSACTION.Text = "* Select Account *"
         End Try
         cbxTRANSACTION.Visible = True
         cbxTRANSACTION.Focus()
      Case Else
            tbxTRANSACTION.Size = grdTRANSACTION.CurrentCell.Size
            tbxTRANSACTION.Location = CellLocation(grdTRANSACTION, grdTRANSACTION.CurrentCellAddress.X, grdTRANSACTION.CurrentCellAddress.Y)
            Select Case grdTRANSACTION.CurrentCellAddress.X
            Case 0
            tbxTRANSACTION.TextAlign = HorizontalAlignment.Center
      Case 2
            tbxTRANSACTION.TextAlign = HorizontalAlignment.Left
      Case 3
            tbxTRANSACTION.TextAlign = HorizontalAlignment.Right
      End Select
      Try
            tbxTRANSACTION.Text = grdTRANSACTION.CurrentCell.Value.ToString
      Catch
            tbxTRANSACTION.Text = ""
      End Try
      tbxTRANSACTION.Visible = True
      tbxTRANSACTION.Focus()
      End Select
      flgGRDEDT = True
   End If
End Sub
 
'****************************************************************************************
Private Sub prcPOPULATE_XXXXXX_CELL()
'****************************************************************************************
      If flgBLDGRD = True And flgGRDEDT = True Then
            flgCELVLD = True
            Select Case grdTRANSACTION.CurrentCellAddress.X
            Case 0
                  hldACTNBR = CLng(tbxTRANSACTION.Text)
                  If hldACTNBR <> 0 Then
                        Call prcSQLXXM("ACCOUNT")
                        If rdrXXM.Read Then
                              grdTRANSACTION.CurrentCell.Value = tbxTRANSACTION.Text
                              grdTRANSACTION.Item(grdTRANSACTION.CurrentCellAddress.X + 1, grdTRANSACTION.CurrentCellAddress.Y).Value = CType(rdrXXM.Item("xxmACTNME"), String)
                        Else
                              MsgBox("The account number entered does not bring up an account.") : flgCELVLD = False
                        End If
                 End If
            Case 1
                  grdTRANSACTION.CurrentCell.Value = cbxTRANSACTION.Text
            Case 2
                  If tbxTRANSACTION.Text.Length = 0 Then
                        MsgBox("A transactions description must be entered.") : flgCELVLD = False
                  Else
                        grdTRANSACTION.CurrentCell.Value = tbxTRANSACTION.Text
                  End If
            Case 3
                  Try
                        grdTRANSACTION.CurrentCell.Value = Format(CSng(tbxTRANSACTION.Text), "#####0.00")
                  Catch
                        MsgBox("The dollar amount entered $" & CStr(tbxTRANSACTION.Text) & " is not a valid dollar amount.") : flgCELVLD = False
                  End Try
            End Select
      If flgCELVLD = True Then
            tbxTRANSACTION.Visible = False
            cbxTRANSACTION.Visible = False
            flgGRDEDT = False
      End If
      End If
End Sub
 
'****************************************************************************************
Private Sub prcNEXT_XXXXXX_CELL()
'****************************************************************************************
If flgCELVLD = True Then
With grdTRANSACTION
Select Case .CurrentCellAddress.X
Case 0
.CurrentCell = .Rows(.CurrentCellAddress.Y).Cells(2)
Case 1
.CurrentCell = .Rows(.CurrentCellAddress.Y).Cells(2)
Case 2
.CurrentCell = .Rows(.CurrentCellAddress.Y).Cells(3)
Case 3
.CurrentCell = .Rows(.CurrentCellAddress.Y + 1).Cells(0)
End Select
End With
End If
End Sub
 
'****************************************************************************************
Private Sub prcAUD_XXXXXX_GRID()
'****************************************************************************************
      Dim X As Integer, wrkACTNBR as Long
      For X = 0 To grdTRANSACTION.RowCount - 1
      hldACTNBR = CLng(grdTRANSACTION.Item(0, X).Value)
      If hldACTNBR > 0 Then
            If X < aryXXT.Count Then
                  wrkACTNBR = CType(aryXXT.Item(X), keyXXT).keyACTNBR
                  hldTRNCNT = CType(aryXXT.Item(X), keyXXT).keyTRNCNT
            Else
                  wrkACTNBR = 0
                  hldTRNCNT = 0
            End If
            hldDESCPT = CStr(grdTRANSACTION.Item(2, X).Value)
            hldTRNAMT = CDbl(grdTRANSACTION.Item(3, X).Value)
            Select Case True
            Case hldACTNBR > 0 And wrkACTNBR = 0
            Call prcSQLXXT("NEWCNT")
                  If rdrXXT.Read() Then
                        hldTRNCNT = CInt(rdrXXT("maxCOUNT")) + 1
                  Else
                        hldTRNCNT = 1
                  End If
                  Call prcSQLXXT("INSERT")
            Case hldACTNBR > 0 And wrkACTNBR > 0
                  Call prcSQLXXT("UPDATE")
            Case hldACTNBR = 0 And wrkACTNBR > 0
                  Call prcSQLXXT("DELETE")
            End Select
      End If
      Next X
End Sub