Articles in this section
Category / Section

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

6 mins read

Cell Spanning:

You could use the CoveredRange support to acheive Cell Spanning WinForms GridDataBoundGrid. 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


Conclusion

I hope you enjoyed learning about how do I make Cell Spanning ,Cell Spacing and Cell Padding in WinForms GridDataBoundGrid.

You can refer to our WinForms GridDataBoundGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied