How to Clear All or Part of Grid in Visual Basic (88911)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Professional Toolkit for Microsoft Visual Basic programming system for Windows

This article was previously published under Q88911

SUMMARY

You can clear all or part of a grid by first selecting the region to clear using the SelStartCol, SelStartRow, SelEndCol, and SelEndRow properties, and then clearing the region by assigning a null string to the Clip property.

MORE INFORMATION

The example below demonstrates how to clear all the non-fixed cells of a grid.

Step-by-Step Example

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
  2. From the File menu, choose Add File, and select GRID.VBX. The Grid tool appears in the Toolbox. (This step is automatic in version 2.0.)
  3. Place a grid (Grid1) on Form1.
  4. Set the Grid1 property Cols to 4, and Rows to 4. Size the grid so that you can see all the cells.
  5. Enter the following code into the Form1 Load event procedure:
       Sub Form_Load ()
          ' Load some data into the grid.
          For i% = Grid1.FixedCols To Grid1.Cols - 1
             For j% = Grid1.FixedRows To Grid1.Rows - 1
                Grid1.Col = i%
                Grid1.Row = j%
                Grid1.Text = Format$(i% + j%)
             Next
          Next
       End Sub
    						
  6. Enter the following code into the Form1 Click event procedure:
       Sub Form_Click ()
          ' Select all non-fixed grid cells.
          Grid1.SelStartCol = Grid1.FixedCols
          Grid1.SelStartRow = Grid1.FixedRows
          Grid1.SelEndCol = Grid1.Cols - 1
          Grid1.SelEndRow = Grid1.Rows - 1
    
          ' Clear the cells.
          Grid1.Clip = ""
    
          ' Clean up the grid.
          Grid1.Col = Grid1.FixedCols
          Grid1.Row = Grid1.FixedRows
          Grid1.SelEndCol = Grid1.SelStartCol
          Grid1.SelEndRow = Grid1.SelStartRow
       End Sub
    						
  7. Press F5 to run the program. The grid appears with numbers in the cells. Click Form1. The grid is cleared.

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:KB88911