The grid control lets you display information in a series of rows and
columns, including special rows and columns that display row and
column headings. The most common example of a program which deals
with this type of information display is a spreadsheet.
Visual Basic has two separate controls for working with rows and
columns of information:
Dbgrid control for displaying and editing the contents of a database
MsflexGrid control is capable of displaying text or pictures in any of its
cells. Moreover, you can use the grid to sort information in the tables and
format the information for easier viewing.
Possible Uses
The Microsoft FlexGrid control can be used in the following scenarios:
• To implement a "sorting and merging" data display that groups
information by category, allowing the user to modify the order in
which information is presented.
• To create a spreadsheet with in-cell editing using standard Visual
Basic controls.
• To implement an outline-style display, with heading items that can be
collapsed or expanded with the mouse.
If the number of rows and columns is greater than can fit on-screen, the
FlexGrid automatically provides scrollbars. The headers are controlled by
the FixedRows and FixedCols properties.
Property description
Rows Sets or returns the total number of fixed and non-fixed rows in the
grid
Cols Sets or returns the total number of fixed and non-fixed Columns in
the grid
FixedRows Sets or returns the number of fixed rows in the grid
Background Foreground
Color Property Color Property Color Use
BackColor ForeColor Controls the color of any standard
cells in the grid. A standard cell is
one that is not part of the fixed
rows or columns and is not
selected.
To change the appearance of an individual cell, you can use the color and
font properties in conjunction with the row and col properties.
For example, the following code selects the cell in row 3, column 4 , and
then changes it to a Bold Red font.
FgMain.Row =3
FgMain.col = 4
FgMain.text = “Bold Font”
FgMain.CellFontBold = True
FgMain.CellForeColor =VbRed
FlexGrid control gives you control over the lines that make up the grid.
These lines are controlled by the following properties.
GridColor :sets the color of the grid lines in all standard cells
GridColorFixed : sets the color of the grid lines in the fixed cells
GridLines : Sets the apperance of the grid lines for the standard cells.
0 – flexGridNone(no grid lines)
1 – flexGridFlat (Flat grid lines)
2 – flexGridInset (inset grid lines)
3 – flexGridRaised (raised grid lines)
GridLinesFixed : sets the appearance of the grid lines in the fixed cells.
GridLineWidth : sets the width of the lines use to make up the grid.
SelectionMode :Determines how the user can select cells in the grid.
0 – allow user to select cells individually
1 – forces the selections to cover entire rows
2 – forces the selections to cover entire columns
There are three properties that you can use to set or retrieve the value of a
cell in the FlexGrid: Text, TextArray, and TextMatrix. You can use
each of these properties to retrieve or set the values of a single cell.
The following code shows how you would retrieve the value of the cell in
the second row and second column of the grid.
FgMain.Row = 1
Fgmain.Col = 1
TxtReturn = FgMain.Text
If a single cell is selected, text property only sets the value of the selected
set. If multiple cells are selected, the text property will set the value of the
current cell if FillStyle property is set to 0, or will set the value of all the
selected cells if the FillStyle property is set to 1.
You can only retrieve or set the value of the current cell. This means if
you want to process multiple cells, you will have to set the Row and Col
properties for each cell`s value you want to retrieve.
FgMain.Row = 1
FgMain.Col = 1
Fgmain.Text = “One Cell”
Fgmain.Col = 2
FgMain.text = “Another Cell”
TextArray property can be used to retrieve the value of any cell, not just
the current cell but for working with multiple cells. The textarray
property uses a single index to specify the cell to be retrieved. This index
is determined by multiplying the desired row number by the number of
columns in the grid and then adding the desired column number. The
following code shows how to retrieve the value from the cell in the third
column of the second row.
InDesiredCol = 2
InDesiredRow = 1
TxtRet = Fgmain.TextArray(InDesiredRow * Fgmain.Col + IndesiredCol)
TextMatrix property is used for setting and retrieving the value of a cell.
It requires two arguments. The index of the row and the index of the
column to be retrieved. Following is used to retrieve the value from the
cell in the third column of the second row.
Txtreturn = Fgmain.TxtMatrix(1 , 2)
Using AddItem method you can specify index value for the method to
insert the row somewhere else in the grid. This method works the same
way as a list box`s AddItem method. The following lines of code show
how the AddItem method can be used:
FgMain.AddItem “NemRoman”
FgMain.AddItem “NewRoman”, 2
FgMain.AddItem “NewRoman” & Chr(9) & “ NewRoman”
To specify the values for multiple cells, create a text string containing the
values of all the input cells, in column order. Between each pair of
values, insert the Tab character by using the Chr(9) function.
To delete a specific row from the grid, you use removeItem method,
which requires you to specify the row number of the row to be removed.
The following code shows you how to remove the second row of the grid.
FgMain.RemoveItem 1
To clear the entire grid, you use the clear method, as shown in the
following line
MsFlexGrid1.Clear
Setup
The following controls are used in the scenario:
• Microsoft FlexGrid control
Set Properties of Microsoft FlexGrid Control
In this example, the following properties are used to set the number of
columns and rows, the font and font size, and to create headings for the
columns:
Object Property Setting
Sub Form_Load ()
Dim I As Integer
' Create array.
For i = Fg1.FixedRows To Fg1.Rows - 1
' Region.
Fg1.TextArray(fgi(i, 0)) = RandomString(0)
' Product.
Fg1.TextArray(fgi(i, 1)) = RandomString(1)
' Employee.
Fg1.TextArray(fgi(i, 2)) = RandomString(2)
Fg1.TextArray(fgi(i, 3)) = _
Format(Rnd * 10000, "#.00")
Next
' Set up merging.
Fg1.MergeCol(0) = True
Fg1.MergeCol(1) = True
Fg1.MergeCol(2) = True
' Sort to see the effects.
DoSort
End Sub
Sub DoSort ()
Fg1.Col = 0
Fg1.ColSel = Fg1.Cols - 1
Fg1.Sort = 1 ' Generic ascending.
End Sub
Define Routine to Enter the Data into Microsoft FlexGrid
Next, define a routine that populates the Microsoft FlexGrid control with
sample data:
Function RandomString (kind As Integer)
Dim s As String
Select Case kind
Case 0 ' Region.
Select Case (Rnd * 1000) Mod 5
Case 0: s = "1. Northwest"
Case 1: s = "2. Southwest"
Case 2: s = "3. Midwest"
Case 3: s = "4. East"
Case Else: s = "5. Overseas"
End Select
Case 1 ' Product.
Select Case (Rnd * 1000) Mod 5
Case 0: s = "1. Wahoos"
Case 1: s = "2. Trinkets"
Case 2: s = "3. Foobars"
Case Else: s = "4. Applets"
End Select
Case 2 ' Employee.
Select Case (Rnd * 1000) Mod 4
Case 0: s = "Mary"
Case 1: s = "Sarah"
Case 2: s = "Donna"
Case Else: s = "Paula"
End Select
End Select
RandomString = s
End Function
If you run the project at this point, it should look something like this:
Allow Microsoft FlexGrid to Switch Views in Terms of Data
Organization
To allow the user to reorganize the data by dragging columns to a new
position, add the following two routines.
This routine uses the Tag property to save the column number when the
user presses the mouse button, triggering the MouseDown event:
Sub Fg1_MouseDown (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Fg1.Tag = ""
If Fg1.MouseRow <> 0 Then Exit Sub
Fg1.Tag = Str(Fg1.MouseCol)
MousePointer = vbSizeWE
End Sub
This routine readjusts the columns and sorts the data when the user
releases the mouse button, triggering the MouseUp event:
Sub Fg1_MouseUp (Button As Integer, Shift As _
Integer, X As Single, Y As Single)
MousePointer = vbDefault
If Fg1.Tag = "" Then Exit Sub
Fg1.Redraw = False
Fg1.ColPosition(Val(Fg1.Tag)) = Fg1.MouseCol
DoSort
Fg1.Redraw = True
End Sub
At run time, the data is automatically reorganized when the user drags a
column to a new position. For example, if the Employee column were
dragged to the left, it would then look like this:
Sample Application 2: SpreadSheet With Cell Editing
• Create controls.
• Set properties.
• Add row and column headings.
• Add in-cell editing.
• Add functionality to text box (the "edit" box) for updating the data.
• Copy the data from the text box to Microsoft FlexGrid.
Setup
The following controls are used in the scenario:
• Microsoft FlexGrid control
• Text box control
Create Controls
For this example, add a Microsoft FlexGrid control and then a text box
control inside it to create a parent-child relationship. As in this example:
Set Properties
Set the following properties for the Microsoft FlexGrid and text box
controls:
Object Property Setting
Sub Form_Load ()
Dim i As Integer
Sub Fg2_DblClick ()
MSFlexGridEdit Fg2, txtEdit, 32 ' Simulate a space.
End Sub
To initialize the text box and to pass the focus from the Microsoft
FlexGrid control to the text box, add the following routine:
Sub Fg2_LeaveCell ()
If txtEdit.Visible = False Then Exit Sub
Fg2 = txtEdit
txtEdit.Visible = False
End Sub
At run time, data can be entered into individual cells as in this example:
Setup
The following controls are used in the scenario:
• Microsoft FlexGrid control
Sub Form_Load ()
Dim i As Integer, tot As Integer
Dim t As String, s As String
For i = 0 to 5
Fg3.AddItem s
Next
At run time, the rows are sorted into three divisions under their respective
headings: Air Fare, Meals, and Hotels.
At run time, the user can expand and collapse the row headings by
double-clicking on the "+" or "*" symbols in the first column, as in this
example: