Problem solve Get help with specific problems with your technologies, process and projects.

Flexigrid autofill

This handy tool fills ANY Flexigrid on ANY form with data from ANY recordset.

This handy tool fills ANY Flexigrid on ANY form with data from ANY recordset.
'***********************************************************************************
' Project:      Flexigrid Library Manager
' Module:       Populate
' Author:       Lee Nicholson & Philfr
' Date:         29/4/2004
'               If you like the code and want to use it, please give credit to our
'               work by leaving the above authorship.
'
'***********************************************************************************
' Modified by:
' Date:
' changes made:

'***********************************************************************************
' Description: fills ANY Flexigrid on ANY form with data from ANY recordset.

'              A blank TitleString causes the code to identify and name the columns
'              with the recordset's field names. If the recordset is empty, the
'              flexigrid is displayed with a "No Records To Display" message

'              The ParamArray can contain as many values as there are fields in the
'              recordset.  A zero for any value will cause the column to be hidden.

'              I usually have the KeyField hidden in column Zero, and use the
'              code   flx(flx.MouseRow,0) to identify the record number to search on.
'
'Version: 2.4
'***********************************************************************************

Option Explicit




Public Sub gFillFlexGrid(ByVal RSFG As Recordset, _
                  flx As MSFlexGrid, TitleString As String, SortByColumn As Integer, _
                  ParamArray FieldWidths())
' May 6, 2004
' usage:
'        gFillFlexGrid adoRS, myFlex, "", 0, 1800, 2000, 0, 1000
'                                            |----------> unlimited
'       TitleString takes the form
'           "AssignID|Teacher|^Cluster|Assign Name|Due Date"

Dim i As Integer, MaxRows As Integer
Dim j As Integer, MaxCols As Integer

On Error GoTo erh

If RSFG.RecordCount > 0 Then
    MaxCols = RSFG.Fields.Count
    MaxRows = RSFG.RecordCount
   
    If TitleString = "" Then
    ' derive field names from recordset
      TitleString = RSFG.Fields(0).Name
      For i = 1 To MaxCols - 1
        TitleString = TitleString & "|<" & RSFG.Fields(i).Name
      Next i
    End If
 
    With flx
        .AllowUserResizing = flexResizeColumns
        .Clear
        .FormatString = TitleString
       
        ' set the width of each field on the grid from the FieldWidth array
        For i = 0 To UBound(FieldWidths)
           .ColWidth(i) = FieldWidths(i)
        Next i
       
        ' set the height of each row (assumes the form's mode is in pixels)
        .RowHeight = TextHeight("X") + 10
       
        .Cols = MaxCols
        .Rows = MaxRows + 1
       
        .Row = 0
        ' setting row 0 (Titlestring) to Bold
        For i = 0 To MaxCols - 1
            .col = i
            .CellFontBold = True
        Next i
        .Font.Bold = False
           
        ' fill the rows with data from the recordset
        RSFG.MoveFirst
        j = 1
        Do While Not RSFG.EOF
          For i = 0 To MaxCols - 1
            ' to avoid an error if the field value is NULL add a blank string
            .TextMatrix(j, i) = RSFG.Fields(i) & ""
          Next i
          RSFG.MoveNext
          j = j + 1
        Loop
      
       ' set grid column's to merge and sort on
         For i = 0 To MaxCols - 1
           .MergeCol(i) = True
         Next i
        .MergeCells = flexMergeRestrictColumns
        .ColSel = SortByColumn
        .Sort = flexSortGenericAscending
    End With
   
    Else
        With flx
            .FormatString = "|Data Error"
            .ColWidth(0) = 0
            .ColWidth(1) = 3000
            .Cols = 2
            .Rows = 2
            .TextMatrix(1, 1) = "No Records To Display"
        End With
     End If
  Exit Sub
erh:
   MsgBox Err.Description
End Sub

Dig Deeper on ASP.NET development best practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

SearchCloudApplications

Close