GrapeCity MultiRow Windows Forms Documentation
Automatic Adjustment of the Cell Width

In the GcMultiRow control, you can adjust the width of the cell to fit the cell contents.

Automatic Adjustment based on the Cell Contents

You can use the GcMultiRow.HorizontalAutoSizeMode property to automatically adjust the width of the cells placed in the column header section, column footer section, or row, to fit the cell contents.

Using Code

The following code automatically adjusts the width of the cells placed in the column header section, to fit the cell contents.

[VB]

Imports GrapeCity.Win.MultiRow
Dim textBoxCell1 As New TextBoxCell()
textBoxCell1.Name = "textBoxCell1"
Dim textBoxCell2 As New TextBoxCell()
textBoxCell2.Name = "textBoxCell2"
Dim template1 As Template = Template.CreateGridTemplate(New Cell() {textBoxCell1, textBoxCell2})
template1.ColumnHeaders(0).Cells("columnHeaderCell1").Value = "Column1" 
template1.ColumnHeaders(0).Cells("columnHeaderCell2").Value = "Column headerell"
GcMultiRow1.Template = template1 
GcMultiRow1.RowCount = 3 
GcMultiRow1.SetValue(0, "textBoxCell1", "ABC") 
GcMultiRow1.SetValue(0, "textBoxCell2", "MultiRow for Windows Forms 7.0") 
GcMultiRow1.HorizontalAutoSizeMode = HorizontalAutoSizeMode.CellsInColumnHeader

[CS]

using GrapeCity.Win.MultiRow;
TextBoxCell textBoxCell1 = new TextBoxCell();
textBoxCell1.Name = "textBoxCell1";
TextBoxCell textBoxCell2 = new TextBoxCell();
textBoxCell2.Name = "textBoxCell2";
Template template1 = Template.CreateGridTemplate(new Cell[] { textBoxCell1, textBoxCell2 });
template1.ColumnHeaders[0].Cells["columnHeaderCell1"].Value = "Column1";
template1.ColumnHeaders[0].Cells["columnHeaderCell2"].Value = "Column Header Cell 2";
gcMultiRow1.Template = template1;
gcMultiRow1.RowCount = 3;
gcMultiRow1.SetValue(0, "textBoxCell1", "ABC");
gcMultiRow1.SetValue(0, "textBoxCell2", "MultiRow for Windows Forms 7.0");
gcMultiRow1.HorizontalAutoSizeMode = HorizontalAutoSizeMode.CellsInColumnHeader;

Automatically Adjusting the Column Width of Multiple Columns

If you double-click the right edge of the header, when multiple columns are selected, you can automatically adjust the width of each cell to fit the respective cell contents. There are no additional settings required in order to achieve this behavior.

If you set the Template.LayoutMode property to LeftToRight, and use the Column mode, the width of the row is automatically adjusted, so the behavior, in appearance, seems to be the same as when the Template.LayoutMode property is set to TopToBottom.

Auto-adjustment of Multiple Columns' Width

Disabling Automatic Adjustment

Set the AllowUserToAutoFitColumns property to False to disable the automatic adjustment of the column width, when you double-click the right edge of the header.

Using Code

This example sets the AllowUserToAutoFitColumns property.

[VB]

GcMultiRow1.AllowUserToAutoFitColumns = False

[CS]

gcMultiRow1.AllowUserToAutoFitColumns = false;

Customization of Automatic Adjustment of Width

Use the GcMultiRow.CellAutoFitPreferredSizeNeeded event to customize the cell width that is automatically adjusted, when you double-click the right edge of the header.

Using Code

The following code automatically adjusts the column width to 100, when you double-click the right edge of the header.

[VB]

Imports GrapeCity.Win.MultiRow 
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
Dim textBoxCell1 As New TextBoxCell()
textBoxCell1.Name = "textBoxCell1"
Dim template1 As Template = Template.CreateGridTemplate(New Cell() {textBoxCell1})
GcMultiRow1.Template = template1
GcMultiRow1.RowCount = 3
GcMultiRow1.SetValue(0, "textBoxCell1", "ABC")
End Sub
Private Sub GcMultiRow1_CellAutoFitPreferredSizeNeeded(sender As Object, e As CellAutoFitPreferredSizeNeededEventArgs) Handles GcMultiRow1.CellAutoFitPreferredSizeNeeded 
If e.Scope = CellScope.Row Then 
Dim textBoxCell = DirectCast(GcMultiRow1.Rows(e.SectionIndex).Cells(e.CellIndex), TextBoxCell) 
If textBoxCell IsNot Nothing AndAlso e.Direction = Orientation.Horizontal Then
e.PreferredSize = New Size(100, e.PreferredSize.Height)
End If
End If
End Sub

[CS]

using GrapeCity.Win.MultiRow; 
private void Form1_Load(object sender, EventArgs e) 
{
TextBoxCell textBoxCell1 = new TextBoxCell(); 
textBoxCell1.Name = "textBoxCell1"; 
Template template1 = Template.CreateGridTemplate(new Cell[] { textBoxCell1 }); 
gcMultiRow1.Template = template1; 
gcMultiRow1.RowCount = 3; 
gcMultiRow1.SetValue(0, "textBoxCell1", "ABC"); 
} 
private void gcMultiRow1_CellAutoFitPreferredSizeNeeded(object sender, CellAutoFitPreferredSizeNeededEventArgs e) 
{ 
if (e.Scope == CellScope.Row) 
{
TextBoxCell textBoxCell = gcMultiRow1[e.SectionIndex, e.CellIndex] as TextBoxCell; 
if (textBoxCell != null && 
e.Direction == Orientation.Horizontal) 
{ 
e.PreferredSize = new Size(100, e.PreferredSize.Height); 
} 
} 
}
See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options