Tutorials > Tutorial 27 - Range Selection |
In this tutorial, you will see how you can select cell ranges and perform "cut and paste" functions in True DBGrid.
Start a new project.
From the Visual Basic Project menu, select Components, then check the boxes labeled ComponentOne True DBGrid Pro 8.0 (OLEDB) and Microsoft ADO Data Control (OLEDB).
Place an ADO Data control, a True DBGrid control and a command button on the form.
Next we will connect Adodc1 to the TDBG8Demo database.
Display the custom property pages for Adodc1. Click the General tab and select the Use Connection String option. Click Build. Choose the Microsoft Jet 4.0 OLE DB Provider option. Click Next. Enter the datasource name by pressing the Ellipsis button and locating the database (TDBGDEMO.MDB). Test the connection to make sure it works. Press OK to close the dialog window. Press the Apply button.
Choose the Recordsource tab. Set the Command Type to 2 – adCmdTable and the Table or Stored Procedure Name to Customers. Press the OK button to accept the selections and close the properties page.
Set the TDBGrid1 DataSource to Adodc1.
Change the caption of Command1 to Copy.
Add the following code to Command1:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() Dim rs As ADODB.Recordset ' String to be copied onto clipboard. Dim strTemp As String Dim Col As Integer, row As Variant If TDBGrid1.SelRange Then ' You must format the string so it can be pasted directly into Excel ' (tab delimited). Set rs = Adodc1.Recordset.Clone For Each row In TDBGrid1.SelBookmarks rs.Bookmark = row For Col = TDBGrid1.SelStartCol To TDBGrid1.SelEndCol strTemp = strTemp & rs(Col).Value & vbTab Next Col strTemp = strTemp & vbCrLf Next row Clipboard.Clear Clipboard.SetText strTemp, vbCFText MsgBox "Range of " & CStr(Abs(TDBGrid1.SelEndCol - TDBGrid1.SelStartCol) + 1) _ & " x " & CStr(TDBGrid1.SelBookmarks.Count) & _ " cells have been copied to the clipboard in TAB delimited format" Else MsgBox "Please select a range of cells" End If End Sub |
Go to the True DBGrid property pages and set the MultiSelect property to Extended.
Run the program and observe the following:
Click and drag a group of cells approximately like the example below.
With the cells highlighted press the Copy button.
Open an Excel spreadsheet and paste the cells into Excel.