How to Clear All or Part of Grid in Visual Basic

Last reviewed: June 21, 1995
Article ID: Q88911
The information in this article applies to:
  • Professional Edition of Microsoft Visual Basic for Windows, versions 2.0 and 3.00
  • Microsoft Professional Toolkit for Microsoft Visual Basic programming system for Windows, version 1.0

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.

  • 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.)

  • Place a grid (Grid1) on Form1.

  • Set the Grid1 property Cols to 4, and Rows to 4. Size the grid so that you can see all the cells.

  • 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
    
    

  • 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
    
    

  • Press F5 to run the program. The grid appears with numbers in the cells. Click Form1. The grid is cleared.


  • Additional reference words: 1.00 2.00 3.00
    KBCategory: kbprg kbcode
    KBSubcategory: PrgCtrlsCus


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.