如何在datagridview VB.net中合并单元格

时间:2014-02-26 05:23:14

标签: vb.net

我可以通过手绘来合并细胞,但似乎我对如何正确使用它并不了解

使用for循环我基于上一行的下一行并使文本为空,但每次我将单元格放在第一位时,它就会与它上面的单元格合并。 我希望我能清楚地解释一下...... XD

这是我的代码

        Dim prev As String = dt.Rows(0)(0).ToString() '\\merge with column 0
        Dim prev1 As String = dt.Rows(0)(1).ToString() '\\merge with column 1
        For i = 1 To dt.Rows.Count - 1

            If dt.Rows(i)(0).ToString() = prev Then '\
                dt.Rows(i)(0) = ""                 '\
            Else
                prev = dt.Rows(i)(0).ToString()     '\ column 0 (blank the next row if the same with previous)
            End If                                  '\


            If dt.Rows(i)(1).ToString() = prev1 Then
                dt.Rows(i)(1) = ""                  '\ column 1 (blank the next row if the same with previous)
            Else
                prev1 = dt.Rows(i)(1).ToString()
            End If
        Next

Private Sub DGV1_CellPainting(sender As System.Object,e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)处理DGV1.CellPainting

    If e.RowIndex > 0 Then
        If e.RowIndex < DGV1.Rows.Count - 2 Then
            For col = 0 To DGV1.ColumnCount - 1
                If DGV1.Rows(e.RowIndex + 1).Cells(col).Value.ToString() = "" Then
                    If (e.ColumnIndex = col) Then
                        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None

                    End If
                    'Else
                    '    e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single


                End If


                If DGV1.Rows(e.RowIndex).Cells(col).Value.ToString() = "" Then
                    If (e.ColumnIndex = col) Then
                        e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
                    End If

                 End If
            Next
        End If
    End If

    If e.RowIndex = 0 Then
        For col = 0 To DGV1.ColumnCount - 1
            If DGV1.Rows(e.RowIndex + 1).Cells(col).Value.ToString() = "" Then
                If (e.ColumnIndex = col) Then
                    e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
                End If
            End If

        Next
    End If





End Sub

注意*请忽略列标题

我的输出总是像这样 http://tinypic.com/r/2wqcokj/8

应该是这样的 http://tinypic.com/r/11gstg1/8

0 个答案:

没有答案