Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / Windows Forms
Alternative
Article

DataGridVewTextBoxCell with Span Behaviour

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
1 Jun 2012CPOL2 min read 18.2K   8   7   5
This is an alternative for "DataGridVewTextBoxCell with Span Behaviour"

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. 

VB
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: 

VB
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:

VB
'==========================================================================================
'*
'==========================================================================================
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestioncParam is not declared Pin
Member 1168624210-Jun-15 8:35
Member 1168624210-Jun-15 8:35 
Question.net 2.0 support Pin
Cool Smith22-Jun-13 22:21
Cool Smith22-Jun-13 22:21 
Questionnot bad Pin
BillW3318-Jul-12 3:44
professionalBillW3318-Jul-12 3:44 
QuestionCode enhancement for CellPainting Pin
drunlar11-Jun-12 8:25
drunlar11-Jun-12 8:25 
AnswerRe: Code enhancement for CellPainting Pin
Ammar_Ahmad10-Jun-12 5:43
Ammar_Ahmad10-Jun-12 5:43 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.