DataGrid Merging row

  • Thread starter Thread starter Marco Tibolla
  • Start date Start date
M

Marco Tibolla

How can I merging the rows in my DataGrid in Windows Form?

I'm looking for something like Webform's RowSpam or like MergeRow
FlexGrid's propriety.

Thank you
Marco
 
You can't, you're gonna have to derive a grid and do ALL
the painting yourself or add aditional painting over the
grid to simulate the row mergeing.

Here is a piece of code I wrote a while back in VB.net
and it shows how you can do a merged cell. It merges 4
cells from (2,2) to (3,3). It will merge them and display
them with red background and yellow text. Of course this
only handles painting, there's a lot more work that needs
to be done to implement the whole thing...

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Table1")
Dim dc1 As New DataColumn("Column1")
Dim dc2 As New DataColumn("Column2")
Dim dc3 As New DataColumn("Column3")
Dim dc4 As New DataColumn("Column4")
dt.Columns.Add(dc1)
dt.Columns.Add(dc2)
dt.Columns.Add(dc3)
dt.Columns.Add(dc4)
Dim f As Integer
Dim dr As DataRow
For f = 0 To 20
dr = dt.NewRow()
dr(0) = f * 2
dr(1) = f * 3
dr(2) = f * 4
dr(3) = f * 5
dt.Rows.Add(dr)
Next

Dim dg As New InheritedGrid()
dg.Dock = DockStyle.Fill
dg.DataSource = dt.DefaultView

Dim dts As New DataGridTableStyle(False)
dts.MappingName = "Table1"
For f = 0 To 3
Dim dtbc As New DataGridTextBoxColumnInherited
()
dtbc.MappingName = "Column" & f
dts.GridColumnStyles.Add(dtbc)
Next f

dg.TableStyles.Clear()
dg.TableStyles.Add(dts)


Me.Controls.Add(dg)

End Sub

End Class

Public Class InheritedGrid
Inherits DataGrid

Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
Dim x As Integer
Dim y As Integer
Dim width As Integer
Dim height As Integer
x = Me.GetCellBounds(2, 2).X
y = Me.GetCellBounds(2, 2).Y
width = Me.GetCellBounds(2, 2).Width +
Me.GetCellBounds(3, 3).Width
height = Me.GetCellBounds(2, 2).Height +
Me.GetCellBounds(3, 3).Height
Dim dgc As DataGridTextBoxColumnInherited
dgc = CType(Me.TableStyles
("Table1").GridColumnStyles(2),
DataGridTextBoxColumnInherited)
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(Me.DataSource,
Me.DataMember), CurrencyManager)
dgc.DoPaint(pe.Graphics, New Rectangle(x, y,
width, height), cm, 2, New SolidBrush(Color.Red), New
SolidBrush(Color.Yellow), False)
End Sub

End Class

Public Class DataGridTextBoxColumnInherited
Inherits DataGridTextBoxColumn

Public Sub DoPaint(ByVal g As
System.Drawing.Graphics, ByVal bounds As
System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer, ByVal backBrush As System.Drawing.Brush, ByVal
foreBrush As System.Drawing.Brush, ByVal alignToRight As
Boolean)
Me.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight)
End Sub
End Class
 
Back
Top