The DataView class presents a DB grid view that displays the content of a DB result. You can navigate through the data records using the cursor keys or the mouse. Alternatively, you can insert a navigation bar into the form, but you will have to programme its functionality yourself. You can use the 'Columns' property (data type string array) to specify which DB fields are displayed. If you insert a header row with Header = True, use the 'Labels' property (data type string array) to specify the identifiers of the individual columns. The DB data of the DB table used can be provided directly as a DB result or via the 'Datasource' control element (default).
The DataView class has the following properties:
Property | Data type | Description |
---|---|---|
Columns | String[ ] | Determines the fields whose content is displayed in the columns in the DataView. You can also read the value of the property. If the property is not set, this restriction is missing and all fields are automatically displayed. |
Count | Integer | Delivers the current number of data records. |
Current | Variant[ ] | Returns the values of all primary keys of the current data set as an array of type Variant. |
Data | _GridView_Data | Use this property to display the contents of a cell during the Data event (Event Data ( Row As Integer, Column As Integer, Value As Variant )). |
Editable | Boolean | If the value is True, the contents of all cells in the TableView can be edited. You can also read the value of the property. |
Grid | Boolean | If the value is True, a grid is displayed. You can also read the value of the property. |
Header | Boolean | If the value is True, a header is displayed in the grid. You can also read the value of the property. |
Index | Integer | Delivers the index of the current data set. |
Labels | String[ ] | Replaces the field names in the header row in the grid with the content of the string array in the specified order. You can also read the value of the property. |
Sorted | Boolean | Indicates whether the field values can be sorted by clicking on the column headers in the header or sets the property. |
View | TableView | Returns the table view used for displaying/editing the data. |
Table 22.5.7.1.1 : Properties of the DataView class
Properties, methods and events of a TableView can be found in chapter '17.8 TableView'.
Here are some examples of the use of the View property in a DataView:
DataView1.View.Columns[0].Alignment = Align.Center DataView1.View.Columns[0].Width = 36 DataView1.View.Rows[DataSource1.Index].Selected = True
The two most interesting methods are View.Edit ( [ List As String[], ReadOnly As Boolean ] ) and View.EditWith( Editor As Control ). These two methods must be called in the Click event or in the Activate event. If the optional parameter 'List' is defined, the current cell is provided with a ComboBox from which an entry can be selected from the specified list. If 'ReadOnly' is defined and True, the entries in the ComboBox can only be read - but not changed. Without the optional parameters, a cell is edited with the standard editor (TextBox).
You can change the standard (cell) editor using the EditWith method. However, the alternative editor must be inserted as a control element in the form in which the DataView also exists.
Example of the ViewEdit([ List As String[ ], ReadOnly As Boolean ] ) method:
Public Sub DataView1_Activate() '-- The event is triggered when the current record changes. If DataSource1.Index > -1 Then DataSource1.MoveTo(DataView1.Index) Else Return Endif If ComboboxEditMode.Value = False Then DataView1.View.Edit() Else Select Case DataView1.View.Column Case 0 DataView1.View.Edit(["Linux", "Windows", "OS-X"], True) Case 1 DataView1.View.Edit(["Ubuntu 12.04", "Mint 17", "X.10"], False) Case 2 DataView1.View.Edit(["Desktop-PC", "NoteBook", "Tablet-PC"], True) End Select Endif End
The DataView class has these methods:
Method | Return type | Description |
---|---|---|
Cancel | - | Cancels the processing of the current data set. |
Create | - | Switches to create mode. A new row is displayed at the end of the table view, which represents the newly created empty data set. |
Find(Where As String, …) | - | Moves the current row to the next data record that matches the specified filter. Where is an SQL WHERE clause that the next data record must match. Additional arguments are replaced within the Where string, as with the DB.Subst method. The search starts with the row following the current row or with the first row if there is no current row. |
MoveFirst() | - | Moves the current record record pointer to the first record that is selected. |
MoveLast() | - | Moves the current data set pointer to the last data set. |
MovePrevious() | - | Moves the current record pointer to the previous record. |
MoveNext() | - | Moves the current record pointer to the next record. |
MoveTo(Index As Integer) | - | Moves the current record pointer to the record with the specified index, which is then selected. |
Remove() | Boolean | Deletes the selected data records and returns the function value TRUE if this was not possible. |
Save() | Boolean | Saves the current data set and returns the function value TRUE if this was not possible. |
Table 22.5.7.2.1 : Methods of the DataView class
Two events are important for the DataView class:
Examples:
Public Sub DataView1_Data(Row As Integer, Column As Integer, Value As Variant) If IsNull(Value) Then Return '-- For each line … '-- ID If Column = 0 Then DataView1.Data.Font.Bold = True Endif '-- Name If Column = 1 Then DataView1.Data.Text = Value Endif '-- Date If Column = 2 Then If TypeOf(Value) = gb.Date Then DataView1.Data.Text = Format(Value, gb.MediumDate) Endif Endif End
In the following database application, the DB data is displayed in a DataView and selected DB data (name, date and image) in three DB controls. The image content of the BLOB field in the selected data record is shown in a picture box.
In the illustration, you can see the four buttons for navigating in the DataView on the right below the DataView and the buttons on the left to create a new, empty data record, to refresh the display, to save new or changed data or to delete a data record completely:
Figure 22.5.7.4.1: DB application with DataView and three DB controls
In the lower part, in addition to a search for the contents of a specific field with a LIKE filter, the creation of a database backup (DB dump) is also implemented.
The complete description of the DB project can be found in chapter '22.13 Blob projects'.
Notes
The 'DataSource' container control provides the controls with all data from the selected database to which the connection has been made. You can select which database data should actually be displayed using the Table and Filter properties:
DataSource1.Connection = DBCS.DBConnection DataSource1.Table = "contacts" '-- Filter (optional) -> Where-Klausel: ' DataSource1.Filter = "id > 4 AND city <> 'Berlin' AND city NOT LIKE 'G%'"
22.5.7.5 Source code extracts
The following source code excerpts show the use of the MoveFirst(), MoveNext(), MoveLast() and MovePrevious() methods in the navigation bar:
Public Sub ButtonGoToFirst_Click() '-- At least one data set exists ... If DataSource1.Index > -1 Then DataSource1.MoveFirst() '-- Mark current line DataView1.View.Rows[0].Selected = True DataView1.SetFocus() Endif End Public Sub ButtonGoToLast_Click() If DataSource1.Index > -1 Then DataSource1.MoveLast() DataView1.View.Rows[DataSource1.Count - 1].Selected = True DataView1.SetFocus() Endif End Public Sub ButtonGoToPrevious_Click() If DataSource1.Index > -1 Then DataSource1.MovePrevious() DataView1.View.Rows[DataSource1.Index].Selected = True DataView1.SetFocus() Endif End Public Sub ButtonGoToNext_Click() If DataSource1.Index > -1 Then DataSource1.MoveNext() DataView1.View.Rows[DataSource1.Index].Selected = True DataView1.SetFocus() Endif End