65.9K
CodeProject 正在变化。 阅读更多。
Home

带跨度行为的 DataGridViewTextBoxCell

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.25/5 (3投票s)

2012年6月1日

CPOL

2分钟阅读

viewsIcon

19218

downloadIcon

9

这是“带跨度行为的 DataGridViewTextBoxCell”的替代方案

引言

我将这段不错的代码移植到了 VB.NET。这是一个非常快速和粗略的移植,但它似乎可以完成我需要的一切,所以如果你使用 VB,它可能是一个好的开始 Wink | <img src=  

请注意,我使用了一个网站进行语言转换,并在必要时进行了手动调整,我没有检查代码的整洁性。我所做的只是验证它是否适合我!

我对核心功能做了一个更改。我不喜欢所有合并单元格上的蓝色高亮显示,所以我将其更改为默认颜色,您可以自己设置颜色或将其保留为网格默认颜色。 

我还在主 C# 文件之外添加了一些技巧,使其在某些情况下看起来正确,并且每当选择任何合并单元格时,都会选择整个合并组。 这些对于使用 C# 的人来说可能也很好。 我没有将增强功能放在一个单独的文件中,而是将这些记录在“使用代码”部分中。

我将所有源文件合并到一个文件中,这是个人喜好。

使用代码 

这是跨单元格的核心(就像原始单元格一样,但必须使用 SpannedDataGridView 进行 New)。 一旦创建单元格,您可以跨越行或列。 

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

此代码用于强制选择属于跨越单元格但不是左上角单元格的单元格,以始终选择左上角单元格。这是一个很好的技巧,可以使跨度始终看起来完整。 为此,您必须首先知道您最初使用的需求来确定跨度并适应您的需求。 希望这部分应该在这里很清楚: 

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

我不确定这是否是我的代码移植或其他代码中的错误,但是在我停止绘制跨度内的所有单元格(而不是第一个单元格)之前,跨度没有出现。 这是该代码

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

历史

我对核心功能做了一个更改。我不喜欢所有合并单元格上的蓝色高亮显示,所以我将其更改为默认颜色,以便您可以自己设置颜色或将其保留为网格默认颜色。 

我还在主 C# 文件之外添加了一些技巧,使其在某些情况下看起来正确,并且每当选择任何合并单元格时,都会选择整个合并组。 这些对于使用 C# 的人来说可能也很好。 我没有将增强功能放在一个单独的文件中,而是将这些记录在“使用代码”部分中。

© . All rights reserved.