vb.net 将多条记录分组/合并为一个 gridview 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15099320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:32:42  来源:igfitidea点击:

Group/merge multiple records as one gridview row

asp.netvb.netgridviewrow

提问by sQuijeW

I would like to know how to make multiple records into a single gridview row. Take a look at the following example.

我想知道如何将多条记录放入一个 gridview 行中。看看下面的例子。

Data Source:

数据源:

FAMILY        GROUP              COLOR
 Dog          Poodle              Blue
 Dog          German Shepherd     Red
 Dog          Pitubll             orange
 Cat          Evil                Green
 Cat          Tabby               purple

I am trying to create the following look in a gridview

我正在尝试在 gridview 中创建以下外观

      FAMILY       GROUP              COLOR
===============================================
      |        |   Poodle           |  Blue
ROW1  | DOG    |   German Shepherd  |  Red
      |        |   Pitbull          |  Orange
===============================================
      |        |   Evil             |  green
ROW2  | CAT    |   tabby            |  purple

The Family column (if there are multiple) will appear as ONE cell, with each corresponding sub category appearing as its own respective row.

Family 列(如果有多个)将显示为一个单元格,每个对应的子类别显示为各自的行。

This way when the user selects the row, the whole row of type FAMILY is selected. Each row is based off the FAMILY, so regardless of how many rows may be in group and colors, it would still be ONE family row. Essentially the gridview is mainly to show information about the FAMILY and the GROUP/COLOR is just strictly used for details about the family.

这样,当用户选择该行时,FAMILY 类型的整行都会被选中。每一行都基于 FAMILY,因此无论组和颜色中有多少行,它仍然是一个家庭行。本质上,gridview 主要用于显示有关 FAMILY 的信息,而 GROUP/COLOR 仅严格用于有关家庭的详细信息。

Is this something that should be done in SQL or through the Gridview bind events? How would I accomplish my goal?

这是应该在 SQL 中完成还是通过 Gridview 绑定事件完成?我将如何实现我的目标?

If my explanation has been unclear just let me know and I will try to clarify.

如果我的解释不清楚,请告诉我,我会尽力澄清。

回答by sQuijeW

Thanks to everyone for your input.

感谢大家的投入。

After further research I came across the following, which I would like to share so that there is at least a partial answer to this question.

经过进一步研究,我发现了以下内容,我想分享一下,以便至少对这个问题有部分答案。

The goal was ultimately achieved by merging cells based off rows that contain identical values. I used the following links as references:

该目标最终通过基于包含相同值的行合并单元格来实现。我使用以下链接作为参考:

http://forums.asp.net/t/1053747.aspx/1

http://forums.asp.net/t/1053747.aspx/1

http://www.c-sharpcorner.com/uploadfile/satyapriyanayak/ghrth/

http://www.c-sharpcorner.com/uploadfile/satyapriyanayak/ghrth/

Now i am looking to treat each row (with multiple sub categories) as a single row, which I have not figure out yet.

现在我希望将每一行(具有多个子类别)视为一行,我还没有弄清楚。

Considered this question answered with the above links. the core code is as follows (quick summary) using the given example in the OP.

考虑到通过上述链接回答了这个问题。核心代码如下(快速摘要),使用 OP 中的给定示例。

Data Source:

数据源:

FAMILY        GROUP              COLOR
 Dog          Poodle              Blue
 Dog          German Shepherd     Red
 Dog          Pitubll             orange
 Cat          Evil                Green
 Cat          Tabby               purple

In the gridview rowdatabound, use the following structure (VB.NET)

在gridview rowdatabound中,使用如下结构(VB.NET)

CLASS LEVEL VARIABLES
Protected familyName AS String
Protected familyNameCell AS TableCell

If e.Row.RowType = DataControlRowType.DataRow Then

        If Me.familyName <> e.Row.Cells(0).Text Then
            Me.familyName = e.Row.Cells(0).Text
            Me.familyNameCell = e.Row.Cells(0)

        Else
            'remove cells from any column index
            e.Row.Cells.RemoveAt(0)

            'increase rowspan of any cell in column that had a cell removed
            If Me.familyNameCell.RowSpan = 0 Then
                Me.familyNameCell.RowSpan = 2
            Else
                Me.familyNameCell.RowSpan += 1
            End If
        End If
End If

Cheers

干杯

回答by Valen

you can do what your asking in SQL and return a dataset that you can databind to control

您可以在 SQL 中执行您的要求并返回一个您可以通过数据绑定来控制的数据集

回答by Ceres

I would make a collection of a custom class and bind that to your grid.

我会收集一个自定义类并将其绑定到您的网格。

Dim MyDataList As New List(Of MyData)

'Populate list

MyGrid.Datasource = MyDataList
MyGrid.Databind()

...

...

Public Class MyData

    Public Property Family As String
    Private _Groups As List(Of String)
    Private _Colors As List(Of String)

    Public Property Groups() As String
        Get
            Return String.Join(", ", _Groups)
        End Get
        Set(value As String)
            _Groups.Add(value)
        End Set
    End Property

    Public Property Colors() As String
        Get
            Return String.Join(", ", _Colors)
        End Get
        Set(value As String)
            _Colors.Add(value)
        End Set
    End Property

    Public Sub New(family As String)

        Me._Family = family
        Me._Groups = New List(Of String)
        Me._Colors = New List(Of String)

    End Sub
End Class

回答by Siz S

you can use gridviewhelper for grouping in gridview, Grid View Helper

您可以使用 gridviewhelper 在 gridview、Grid View Helper 中进行分组

written in c#, you can use it in vb as

用c#编写,你可以在vb中使用它作为

 Dim helper As GridViewHelper = New GridViewHelper(sender)
    helper.RegisterGroup("colname", True, True)
    helper.ApplyGroupSort()