ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Tutorial 14 - Creating a Grid with Fixed, Nonscrolling Columns

Often, you would like to prevent one or more columns from scrolling horizontally or vertically so that they will always be in view. The SplitCollection of True DBGrid provides a generalized mechanism for defining groups of adjacent columns, and can be used to implement any number of fixed, nonscrolling columns or rows. In this tutorial, you will learn how to write code to create a grid with two horizontal splits, and then "fix" a pair of columns in the leftmost split.

1.   Follow Tutorial 1 - Binding True DBGrid to a DataSet to create a project with a C1TrueDBGrid bound to a DataSet.

2.   In the Load event for Form1, place the following code to create an additional split and to fix columns 0 and 1 in the leftmost split:

·      Visual Basic

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Create an additional split.

    Me.C1TrueDBGrid1.InsertHorizontalSplit(0)

 

    ' Hide all columns in the leftmost split except 0 and 1.

    Dim x As Integer

    For x = 2 To Me.C1TrueDBGrid1.Columns.Count - 1

        Me.C1TrueDBGrid1.Splits(0).DisplayColumns(x).Visible = False

    Next

 

    ' Configure split 0 to display exactly 2 columns.

    With Me.C1TrueDBGrid1.Splits(0)

        .SplitSizeMode = C1.Win.C1TrueDBGrid.SizeModeEnum.NumberOfColumns

        .SplitSize = 2

        .AllowHorizontalSizing = False

    End With

 

    ' Make columns 0 and 1 invisible in split 1.

    Me.C1TrueDBGrid1.Splits(1).DisplayColumns(0).Visible = False

    Me.C1TrueDBGrid1.Splits(1).DisplayColumns(1).Visible = False

 

    ' Turn off recordselectors in split 1.

    Me.C1TrueDBGrid1.Splits(1).RecordSelectors = False

End Sub

·      C#

private void Form1_Load( System.object sender,  System.EventArgs e)

{

    // Create an additional split.

    this.C1TrueDBGrid1.InsertHorizontalSplit(0);

 

    // Hide all columns in the leftmost split except 0 and 1.

    int x;

    for ( x = 2 ; x < this.C1TrueDBGrid1.Columns.Count; x++)

    {

        this.C1TrueDBGrid1.Splits[0].DisplayColumns[x].Visible = false;

    }

 

    // Configure split 0 to display exactly 2 columns.

    this.C1TrueDBGrid1.Splits[0].SplitSizeMode = C1.Win.C1TrueDBGrid.SizeModeEnum.NumberOfColumns;

    this.C1TrueDBGrid1.Splits[0].SplitSize = 2;

    this.C1TrueDBGrid1.Splits[0].AllowHorizontalSizing = false;

 

    // Make columns 0 and 1 invisible in split 1.

    this.C1TrueDBGrid1.Splits[1].DisplayColumns[0].Visible = false;

    this.C1TrueDBGrid1.Splits[1].DisplayColumns[1].Visible = false;

 

    // Turn off recordselectors in split 1.

    this.C1TrueDBGrid1.Splits[1].RecordSelectors = false;

}

·      Delphi

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

var

  x: Integer;

begin

 

  // Create an additional split.

  C1TrueDBGrid1.InsertHorizontalSplit(0);

 

  // Hide all columns in the leftmost split except 0 and 1.

  x := 2;

  while (x < Self.c1TrueDBGrid1.Columns.Count) do

  begin

    Self.C1TrueDBGrid1.Splits[0].DisplayColumns[x].Visible := False;

    x := x + 1;

  end;

 

  // Configure split 0 to display exactly 2 columns.

  Self.C1TrueDBGrid1.Splits[0].SplitSizeMode := C1.Win.C1TrueDBGrid.SizeModeEnum.NumberOfColumns;

  Self.C1TrueDBGrid1.Splits[0].SplitSize := 2;

  Self.C1TrueDBGrid1.Splits[0].AllowHorizontalSizing := False;

  Self.C1TrueDBGrid1.Splits[1].DisplayColumns[0].Visible := False;

  Self.C1TrueDBGrid1.Splits[1].DisplayColumns[1].Visible := False;

  Self.C1TrueDBGrid1.Splits[1].RecordSelectors := False;

end;

Run the program and observe the following:

·      C1TrueDBGrid displays data from the Data control as in Tutorial 1 - Binding True DBGrid to a DataSet.

·      The two columns (First and Last) in the leftmost split are fixed and cannot be scrolled. In fact, there is no horizontal scroll bar present under the left split. A horizontal scroll bar appears under the rightmost split, allowing users to scroll the columns in this split.

Use splits to create fixed, non-scrolling columns anywhere within the grid—even in the middle. Also use splits to present different views of data. For example, splits can be created that scroll independently (in the vertical direction) so that users may compare records at the beginning of the database with those at the end. For more information, see How to Use Splits.

This concludes the tutorial.


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