In this tutorial, you will learn how to use the grid’s DataView property to display data in uncommon display formats such as Inverted View, GroupBy View, and Form View.
1. Start with the project created in Tutorial 1 - Binding True DBGrid to a DataSet.
2. Add a ComboBox (ComboBox1) to the project, and set its Text property to "Data View".
3. In the Properties window, open up the List editor for the ComboBox by clicking on the ellipsis button next to the Items property. In this editor add the following items:
Normal
Inverted
Form
GroupBy
MultipleLines
Hierarchical
4. Now add the following code to the existing code in the Load event of Form1:
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Normal
Me.ComboBox1.SelectedIndex = 0
· C#
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Normal;
this.ComboBox1.SelectedIndex = 0;
· Delphi
Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.Normal;
Self.ComboBox1.SelectedIndex := 0;
5. Now add the following code to the SelectedIndexChanged event of ComboBox1. It changes the DataView property of the grid for each value the user selects in the ComboBox:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedItem
Case "Normal"
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Normal
Case "Inverted"
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Inverted
Case "Form"
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Form
Case "GroupBy"
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy
Case "MultipleLines"
Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.MultipleLines
Case "Hierarchical"
MessageBox.Show ("Hierarchical View can't be set at run time. Please see theHierarchical Display tutorial")
End Select
End Sub
· C#
private void ComboBox1_SelectedIndexChanged( object sender, System.EventArgs e)
{
switch (ComboBox1.SelectedItem)
{
case "Normal":
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Normal;
break;
case "Inverted":
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Inverted;
break;
case "Form":
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.Form;
break;
case "GroupBy":
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;
break;
case "MultipleLines":
this.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.MultipleLines;
break;
case "Hierarchical";
MessageBox.Show ("Hierarchical View can't be set at run time. Please see the Hierarchical Display tutorial");
break;
}
}
· Delphi
procedure TWinForm.ComboBox1_SelectedIndexChanged(sender: System.Object; e: System.EventArgs);
begin
case ComboBox1.SelectedIndex of
0: Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.Normal;
1: Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.Inverted;
2: Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.Form;
3: Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;
4: Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.MultipleLines;
5: MessageBox.Show('Hierarchical View can't be set at run time. Please see the Hierarchical Display tutorial');
end;
end;
Run the program and observe the following:
· C1TrueDBGrid1 displays the data specified in Tutorial 1 - Binding True DBGrid to a DataSet.
· Change the ComboBox to Inverted. Inverted view shows the grid columns as rows and the grid rows as column. The grid should now look like the following:
· Change the ComboBox to Form. Form view shows each record in a Form-like view that is optimal for data-entry. The grid should now look like the following:
· Change the ComboBox to GroupBy. GroupBy View contains a grouping section above the grid where columns can be dragged. Dragging a column to this area sorts the rest of the grid by this column. Drag the Company column to the grouping area. The grid should now look like the following:
· Change the ComboBox to MultipleLines. The MultipleLines View shows all of the columns in the current grid area, wrapping the columns that will not fit to successive lines. Notice that the three columns that would have spilled off of the grid are now on a second line. The grid should now look like the following:
· Now set the ComboBox to Hierarchical. No changes occur and the Message Box statement included in the event above pops up which is due to the fact that the hierarchical DataView cannot be set at run time. Hierarchical data must be set before the application runs. For more information on this view, see Tutorial 16 - Hierarchical Display.
This concludes the tutorial.
Send comments about this topic to ComponentOne. Copyright © ComponentOne LLC. All rights reserved. |