ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Tutorial 19 - Range Selection

In this tutorial, you will learn how to use the SelectedRows and SelectedCols objects copy a range from the grid in such a format that it can be pasted into Microsoft Excel.

1.   Start with the project created in Tutorial 1 - Binding True DBGrid to a DataSet.

2.   Add a command button to the form, place it in the lower left corner of the form, and set its Text property to “Copy”.

3.   Next add the following code to the Click event of Button1:

·      Visual Basic

' String to be copied to the clipboard.

Dim strTemp As String

 

Dim row As Integer

Dim col As C1.Win.C1TrueDBGrid.C1DataColumn

Dim cols As Integer, rows As Integer

If Me.C1TrueDBGrid1.SelectedRows.Count > 0 Then

    For Each row In Me.C1TrueDBGrid1.SelectedRows

 

        ' Copy everything here.

        For Each col In Me.C1TrueDBGrid1.SelectedCols

            strTemp = strTemp & col.CellText(row) & vbTab

        Next

    strTemp = strTemp & vbCrLf

    Next

    System.Windows.Forms.Clipboard.SetDataObject(strTemp, False)

    MessageBox.Show ("Range of " & Me.C1TrueDBGrid1.SelectedCols.Count & " x " & C1TrueDBGrid1.SelectedRows.Count & " cells have been copied to the clipboard in TAB delimited format")

Else

    MessageBox.Show ("Please select a range of cells")

End If

·      C#

// String to be copied to the clipboard.

string strTemp;

 

int row;

C1.Win.C1TrueDBGrid.C1DataColumn col;

int cols, rows;

if ( this.C1TrueDBGrid1.SelectedRows.Count > 0 )

{

    foreach ( row in this.C1TrueDBGrid1.SelectedRows)

    {

 

        // Copy everything here.

        foreach ( col in this.C1TrueDBGrid1.SelectedCols)

        {

            strTemp = strTemp + col.CellText(row) + "\t";

        }

        strTemp = strTemp + “\n”;

     }

    System.Windows.Forms.Clipboard.SetDataObject(strTemp, false);

    MessageBox.Show ("Range of " + this.C1TrueDBGrid1.SelectedCols.Count.ToString() + " x " + C1TrueDBGrid1.SelectedRows.Count.ToString() + " cells have been copied to the clipboard in TAB delimited format");

}

else

{

    MessageBox.Show ("Please select a range of cells");

}

·      Delphi

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);

var

  StrTemp: string;

  i, j, Row: Integer;

  Col: C1.Win.C1TrueDBGrid.C1DataColumn;

begin

  StrTemp := '';

  if (C1TrueDBGrid1.SelectedRows.Count > 0) then

  begin

    for i := 0 to C1TrueDBGrid1.SelectedRows.Count-1 do

    begin

      Row := c1TrueDBGrid1.SelectedRows[i];

 

      // Copy everything here.

      for j := 0 to C1TrueDBGrid1.SelectedCols.Count-1 do

      begin

        Col := C1TrueDBGrid1.SelectedCols[j];

        StrTemp := StrTemp + col.CellText(Row) + #9;

        strTemp := strTemp + #13;

      end;

    end;

    System.Windows.Forms.Clipboard.SetDataObject(StrTemp, False);

    MessageBox.Show('Range of ' + Self.C1TrueDBGrid1.SelectedCols.Count.ToString + ' x ' + Self.C1TrueDBGrid1.SelectedRows.Count.ToString + ' cells have been copied to the clipboard in TAB delimited format');

  end

  else

    MessageBox.Show('Please select a range of cells');

end;

Run the program and observe the following:

·      C1TrueDBGrid1 displays the data specified in Tutorial 1 - Binding True DBGrid to a DataSet.

·      If you select a range of cells in the True DBGrid, then press the copy button a message box will appear detailing the cells that you have copied to the clipboard.

·      Now open Microsoft Excel. Select the exact amount of row and column cells as you’ve selected in the True DBGrid, then press paste. You will see that the cells that you copied in the grid are now pasted into Microsoft Excel

This concludes the tutorial.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.