Introduction
I ported this nice piece of code to VB.NET. It was a very
fast and dirty port however it appears to do everything I need so it might be a
good start if you are using VB
Please note I used a website to do the language conversion, made
manual adjustments where necessary, I didn't check over the code for
cleanliness. All I did was verified it worked for me!
I made one change to the core functionality. I didn't like the blue highlight on all merged cells so I changed it to
the default color, you can set the color yourself or leave it to the grid default.
I also added a few tricks outside the main C# file to make it look correct in certain scenarios and whenever any of the merged cells are selected the whole merged group is selected. These might be nice for people using C# as well. Rather than making the enhancements a separate file I documented these in the "Using the Code" section.
I merged all of the sources into
a single file, personal preference.
Using the code
This is the core of spanning cells (just like the original but must be
New
ed with SpannedDataGridView
). You can span rows or cols just the same once you create the cell.
Dim cell As New SpannedDataGridView.DataGridViewTextBoxCellEx
Value = Grid.Rows.Item(iRow).Cells(0).Value
Grid.Rows(iRow).Cells(0) = cell
cell.ColumnSpan = 2
cell.Value = Value
This code is used to force selection of a cell that is part of the spanned cell yet not the top left cell to always select
the top left cell. It is a good hack that will make the span always look complete. In order to do this, you must know the requirement you used in the first place to determine
the span and adapt for your needs. Hopefully that part should be clear here:
Delegate Sub Grid_SetColumnIndex(ByVal i As Integer)
Private Sub <<Name_of_your_Grid>>_EditingControlShowing(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles _
<<Name_of_your_Grid>>.EditingControlShowing
If cParams.cGridVerification.GetIsRowComment(sender.CurrentRow.Index) And _
sender.CurrentCell.ColumnIndex <> 0 Then
sender.BeginEdit(False)
Dim method As New GridOptions_SetColumnIndex(AddressOf GridOptions_BeginEditOfCell)
sender.BeginInvoke(method, 0)
End If
End Sub
Private Sub Grid_BeginEditOfCell(ByVal columnIndex As Integer)
<<Name_of_your_Grid>>.CurrentCell = <<Name_of_your_Grid>>.CurrentRow.Cells(columnIndex)
<<Name_of_your_Grid>>.BeginEdit(True)
End Sub
I am not sure if this was a bug in my code port or other code I have in the way, however the span didn't show up until I stopped painting all cells within the span that wasn't the first cell.
Here is the code for that:
Private Sub <<Name_of_your_Grid>>_CellPainting(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles _
<<Name_of_your_Grid>>.CellPainting
If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
If cParams.cGridVerification.GetIsRowComment(e.RowIndex) Then If e.ColumnIndex <> 0 _
Then e.Handled = True
End If
End Sub
History
I made one change to the core functionality. I didn't like the blue highlight on all merged cells so I changed it to default color so you can set the color yourself or leave it grid default.
I also added a few tricks outside the main C# file to make it look correct in certain scenarios and whenever any of the merged cells are selected the whole merged group is selected. These might be nice for people using C# as well. Rather than making the enhancements a separate file
I documented these in the "Using the Code" section.