We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How do I make Cell Spanning ,Cell Spacing and Cell Padding in GridDataBoundGrid?.

 

Cell Spanning:

You could use the CoveredRange support to acheive Cell Spanning. Please refer this code snippet:

C#

// Spans (2,1) and (3,1) cells---------Row Spanning

this.gridDataBoundGrid1.Model.CoveredRanges.Add(GridRangeInfo.Cells(2,1,3,1));

this.gridDataBoundGrid1[2,1].Text="This spans 2 rows";

//Spans (6,1) and (6,2) cells---------Column Spanning

this.gridDataBoundGrid1.Model.CoveredRanges.Add(GridRangeInfo.Cells(6,1,6,2));

this.gridDataBoundGrid1[6,1].Text="This spans 2 columns";

VB

'Spans (2,1) and (3,1) cells---------Row Spanning

Me.gridDataBoundGrid1.Model.CoveredRanges.Add(GridRangeInfo.Cells(2,1,3,1))

Me.gridDataBoundGrid1(2,1).Text="This spans 2 rows"

'Spans (6,1) and (6,2) cells---------Column Spanning

Me.gridDataBoundGrid1.Model.CoveredRanges.Add(GridRangeInfo.Cells(6,1,6,2))

Me.gridDataBoundGrid1(6,1).Text="This spans 2 columns"

Cell Padding:

You can set the GridStyleInfo's TextMargins.Left / TextMargins.Right property. For individual cells, you have to set the properties by handling the PrepareViewStyleInfo. Please refer the following code snippet:

C#

// Cell padding for all the cells in a column

this.gridDataBoundGrid1.Binder.InternalColumns["Col4"].StyleInfo.TextMargins.Top=15;

this.gridDataBoundGrid1.Binder.InternalColumns["Col4"].StyleInfo.TextMargins.Left=30;

this.gridDataBoundGrid1.Binder.InternalColumns["Col4"].StyleInfo.HorizontalAlignment=GridHorizontalAlignment.Center;

// Cell padding for a particular cell

// Subcribe to PrepareViewStyleInfo

this.gridDataBoundGrid1.PrepareViewStyleInfo += new Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventHandler(this.gridDataBoundGrid1_PrepareViewStyleInfo);

// Event Handler

private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e)

{

// Set the Textmargins for (1,1)

if(e.RowIndex==1 && e.ColIndex==1)

{

e.Style.TextMargins.Top=15;

e.Style.TextMargins.Left=30;

// Align the text to center

e.Style.HorizontalAlignment=GridHorizontalAlignment.Center;

}

}

VB

Cell padding for all the cells in a column

Me.gridDataBoundGrid1(1,4).Text="Custom Borders"

Me.gridDataBoundGrid1.Binder.InternalColumns("Col4").StyleInfo.TextMargins.Top=15

Me.gridDataBoundGrid1.Binder.InternalColumns("Col4").StyleInfo.TextMargins.Left=10

Me.gridDataBoundGrid1.Binder.InternalColumns("Col4").StyleInfo.HorizontalAlignment=GridHorizontalAlignment.Center

' Cell padding for a particular cell

' Subcribe to PrepareViewStyleInfo

//Event Handler

Private Sub gridDataBoundGrid1_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs) Handles gridDataBoundGrid1.PrepareViewStyleInfo

'Set the Textmargins for (1,1)

If (e.RowIndex=1) AndAlso (e.ColIndex=1) Then

e.Style.TextMargins.Top=15

e.Style.TextMargins.Right=50

'Align the text to center

e.Style.HorizontalAlignment=GridHorizontalAlignment.Center

End If

End Sub

Cell Spacing:

For Cell Spacing, you need to handle the DrawCellFrameAppearance event and draw custom borders between adjacent cells. Please refer the following code snippet:

C#

//Event Handler

private void gridDataBoundGrid1_DrawCellFrameAppearance(object sender, syncfusion.Windows.Forms.Grid.GridDrawCellBackgroundEventArgs e)

{

int rowIndex =e.Style.CellIdentity.RowIndex;

int colIndex = e.Style.CellIdentity.ColIndex;

if (rowIndex > 0 && colIndex > 0)

{

Brush brush;

Graphics g = e.Graphics;

brush = new System.Drawing.Drawing2D.LinearGradientBrush(e.TargetBounds, Color.FromArgb( 0, 0, 0 ), Color.FromArgb( 255, 250,250 ), 35f);

// Draw custom border for the cell. Space has been reserved for this

// area with the TableStyle.BorderMargins property.

Rectangle rect = e.TargetBounds;

rect.Inflate(-2, -2);

Rectangle[] rects = new Rectangle[]

{

new Rectangle(rect.X, rect.Y, rect.Width, 4),

new Rectangle(rect.X, rect.Y, 4, rect.Height),

new Rectangle(rect.Right-4, rect.Y, 4, rect.Height),

new Rectangle(rect.X, rect.Bottom-4, rect.Width, 4),

};

g.FillRectangles(brush, rects);

e.Cancel = true;

}

}

VB

'Event Hanlder

Private Sub gridDataBoundGrid1_DrawCellFrameAppearance(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridDrawCellBackgroundEventArgs) Handles gridDataBoundGrid1.DrawCellFrameAppearance

Dim rowIndex As Integer =e.Style.CellIdentity.RowIndex

Dim colIndex As Integer = e.Style.CellIdentity.ColIndex

If rowIndex > 0 AndAlso colIndex > 0 Then

Dim brush As Brush

Dim g As Graphics = e.Graphics

brush = New System.Drawing.Drawing2D.LinearGradientBrush(e.TargetBounds, Color.FromArgb(0, 0, 0), Color.FromArgb(25, 250,250), 35f)

' Draw custom border for the cell. Space has been reserved for this

' area with the TableStyle.BorderMargins property.

Dim rect As Rectangle = e.TargetBounds

rect.Inflate(-2, -2)

Dim rects As Rectangle() = New Rectangle() {New Rectangle(rect.X, rect.Y, rect.Width, 4), New Rectangle(rect.X, rect.Y, 4, rect.Height), New Rectangle(rect.Right - 4, rect.Y, 4, rect.Height), New Rectangle(rect.X, rect.Bottom - 4, rect.Width, 4)}

g.FillRectangles(brush, rects)

e.Cancel = True

End If

End Sub

Here is the link with both CS and VB samples: http://websamples.syncfusion.com/samples/KB/Grid.Windows/GDBG_Cellspan/main.htm

Article ID: Published Date: Last Revised Date: Platform: Control:
4179 02/28/2015 02/28/2015 WinForms GridDataBoundGrid(Classic)
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon