MSFlexGrid Tips and TricksFrank Sommer Microsoft Regional Director, Northern California October 1997 From Regional Director Magazine IntroductionMicrosoft® Visual Basic® version 5.0 comes with not one, not two, but three grid controls in the box. The three grids are Grid, DBGrid, and MSFlexGrid. While I'm tempted to say it took them three tries to get it right, the truth is that there are good reasons for the inclusion of all three. After briefly exploring these reasons, and when to use each grid, this article will take an in-depth look at the newest of the three: MSFlexGrid. Grid is the original Visual Basic grid. It was shipped with the Professional Toolkit for Visual Basic 1.0 back in 1991. DBGrid is a version of the Apex TrueGrid control, and was first shipped with Visual Basic 4.0 in 1995. The newest of the three is MSFlexGrid, a version of the VideoSoft VSFLEX control, and was shipped with Visual Basic 5.0. In the two-grid world of Grid and DBGrid, Grid was favored for its simplicity, and at one-sixth the size of DBGrid, its small footprint. While you will occasionally find a fan of this minimalist grid, for the most part Grid has been ignored by programmers. Its presence in today's toolbox is largely justifiable only for backwards compatibility. When MSFlexGrid is added to the mix, even this reason goes away, as MSFlexGrid supports nearly all of the properties, methods and events of Grid. At only twice the size, its greatly enhanced feature set makes an excellent replacement for the Grid control in existing projects. The real question as to which grid to use boils down to a choice between DBGrid and MSFlexGrid. Both grids are full-featured and highly customizable. There are a few obvious points of departure. If you need full data binding choose DBGrid, as the binding in MSFlexGrid is read-only binding. On the other hand, if you need cell merging or data pivoting, MSFlexGrid is your only choice. Beyond that, each grid has its own look and feel, though MSFlexGrid has a clear advantage in its smaller size. MSFlexGrid is very flexible (hence its name) in the ways you can use it. What follows is a discussion, with some code examples, of various techniques you can use with MSFlexGrid. Getting Data into the Grid There are numerous ways to get data into the MSFlexGrid. There is no "officially" preferred method or clear speed differences, so the proper one to use depends on the source of the data and personal taste. For data contained in a database, the obvious choice is to simply bind the grid to a Data control. Once you set the DatabaseName and RecordSource properties of the Data control, simply use the DataSource property of the MSFlexGrid to bind the grid to the Data control. The grid will populate automatically with data, one row for each record in the database. The first row will contain column headings derived from the field names in the database. If you don't want the automatically created header row, use the RemoveItem method to delete it. Since MSFlexGrid won't allow you to remove a header (fixed) row, you will have to first set the FixedRows property to zero. In its unbound mode, MSFlexGrid is a data container: you put the data in once, and it remains there for the life of the application. There are four properties and one method to help you do this: the AddItem method, and the Clip, Text, TextArray, and TextMatrix properties. If you are using the grid as a listbox, or prefer to think of it as a listbox, use the AddItem method to populate it. The following code will create a two-column grid with the letters A through Z, and their ASCII codes. MSFlexGrid1.Rows = 0For I = Asc("A") To Asc("Z") MyData = Chr$(I) & vbTab & I MSFlexGrid1.AddItem MyDataNext
The AddItem method will add 26 rows to those already in the grid. We set the rows property to zero before starting so that the finished grid will have only 26 rows total. Note how the embedded tab character (vbTab) is used to separate the data for the second column. The AddItem method also has an optional index parameter that allows you to add the row at a specific location. The Clip property is useful to quickly populate a grid with a range of data from any data source that has a compatible Clip method. An rdoResultset is just such a source, and the GetClipString method does the trick. The code sample below will take the first four rows of the Customers table and copy them into rows five through eight of a grid: Dim rs As rdoResultsetSet rs = MyConnection.OpenResultset("Customers")'select the target rangeMSFlexGrid1.Row = 5MSFlexGrid1.Col = 1MSFlexGrid1.RowSel = 8MSFlexGrid1.ColSel = rs.rdoColumns.Count'get the dataMSFlexGrid1.Clip = rs.GetClipString(4) 'returns 4 rows
Notice that we did not have to deal with the data at the Field level, the Clip property did all of that for us. The Clip property returns data as well as setting it, so you can use it to cut and paste from one area of a grid to another. The Text property is frequently used to set the contents of a single cell. That cell however must be the current cell, so you must first use the Row and Col properties to select the desired cell. To set a range of cells to the same value, use the FillStyle property in conjunction with the Text property. The code below puts an "X" in each cell of a
Поиск по форуму
|