如何在VB.NET中对datagridview行进行分组 [英] How to group datagridview Rows in VB.NET

查看:98
本文介绍了如何在VB.NET中对datagridview行进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我对如何在datagridview中对行进行分组存在这个问题。我希望我的datagridview显示像我上传的图像这样的数据,如果你想看一下图像;

I have this problem on how to group rows in a datagridview. I want my datagridview to display data like image i uploaded, if you would mind to take a look at the image;

我已经尝试了下面的代码,它工作正常,但我希望我的datagridview检索数据,如上图;

I've already tried the code below, it works fine but i want to my datagridview retrieve data like the image above;

    Private Sub dgvCurriculum_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvCurriculum.CellFormatting

        If e.RowIndex > 0 And e.ColumnIndex = 0 Then
            If dgvCurriculum.Item(0, e.RowIndex - 1).Value = e.Value Then
                e.Value = ""
            ElseIf e.RowIndex < dgvCurriculum.Rows.Count - 1 Then
                dgvCurriculum.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
            End If
        End If
    End Sub


我知道这听起来很简单但是,我找不到另一种方法来解决这个问题。我是vb.net的新用户。

I know it sounds simple but, i couldn't find another way on how to do it. I'm just new in vb.net.

推荐答案

您可以获得的最佳效果如下所示。否则你需要一个自定义的第三方DataGridView。
以下链接(我多年前做过)显示了如何操作,如下所示。该代码使用自定义(免费)DataGridView,它从xml文件加载数据,但可以来自任何可行的数据源,例如
作为DataSet或DataTable。分组在DataGridView的第一列提要中完成,在本例中为LastName。

The best you can get here is as shown below. Otherwise you would need a custom third party DataGridView. The following link (I did many years ago) shows how to do as shown below. The code uses a custom (free) DataGridView which loads data from a xml file but could come from any viable data source such as a DataSet or DataTable for instance. The grouping is done on the first column feed to the DataGridView, in this case LastName.

Public Class GroupByGrid
    Inherits DataGridView

    Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
        MyBase.OnCellFormatting(args)

        ' First row always displays
        If args.RowIndex = 0 Then
            Return
        End If

        If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
            args.Value = String.Empty
            args.FormattingApplied = True
        End If
    End Sub
    Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal colIndex As Integer) As Boolean
        Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)
        Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)

        If (currCell.Value Is prevCell.Value) OrElse (currCell.Value IsNot Nothing AndAlso prevCell.Value IsNot Nothing AndAlso currCell.Value.ToString() = prevCell.Value.ToString()) Then
            Return True
        Else
            Return False
        End If

    End Function
    Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)

        MyBase.OnCellPainting(args)

        args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None

        ' Ignore column and row headers and first row
        If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
            Return
        End If

        If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
            args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
        Else
            args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
        End If

    End Sub
End Class


这篇关于如何在VB.NET中对datagridview行进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆