Click here to Skip to main content
Click here to Skip to main content

DataGridVewTextBoxCell with Span Behaviour

By , 1 Jun 2012
 

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 Wink | <img src=  

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 Newed 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 its a comment row and not cell 0 then force the event to be cell 0 completing the
    '  illusion of this hack   ;-).  Or meets the the span requirements and isn't top left
    '  cell, adjust as needed. 
    If cParams.cGridVerification.GetIsRowComment(sender.CurrentRow.Index) And _
        sender.CurrentCell.ColumnIndex <> 0 Then 
        'Cencel edit on the current cell
        sender.BeginEdit(False)
        'You cannot directly start the edit of another cell once you stoped the edit of a
        '  current cell, this gets around that issue.
        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 its a comment row and not cell 0 consume the event else paint will override the
        '  cell change
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

drunlar1
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionnot badmemberCIDev18 Jul '12 - 3:44 
Not a bad article, it would be better with source code to download.
Just because the code works, it doesn't mean that it is good code.

QuestionCode enhancement for CellPaintingmemberdrunlar11 Jun '12 - 8:25 
If you selected a cell within the span other than cell 0 everything looked and worked correctly however only cell 0 was highlighted. This issue slightly broke the illusion of the span. This fixes that issue. The code for the fix is commented:
 
'==========================================================================================
'*
'==========================================================================================
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 it's a spanned row and not cell 0 consume the event else paint will override visual
        '  change of the span.
        If cParams.cGridVerification.GetIsRowComment(e.RowIndex) And e.ColumnIndex <> 0 Then
            'If the cell is in a selected state unselect it and select cell 0 of the span
            '  instead. Without this if you select a cell other than 0 the span looks and
            '  functions correctly however the highlight is only on cell 0 slightly breaking
            '  the illusion of this span.
            If sender.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected Then
                sender.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = False
                sender.Rows(e.RowIndex).Cells(0).Selected = True
            End If
            e.Handled = True
        End If
    End If
End Sub
 
I wish I could just update my original post.
AnswerRe: Code enhancement for CellPaintingmemberAmmar_Ahmad10 Jun '12 - 5:43 
You can update it. Just click on "Update your article" it is on the top right hand side, under the search bar.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 1 Jun 2012
Article Copyright 2012 by drunlar1
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid