sQuijeW
sQuijeW

Reputation: 194

Group/merge multiple records as one gridview row

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

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

      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.

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.

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

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

Upvotes: 1

Views: 13759

Answers (4)

sQuijeW
sQuijeW

Reputation: 194

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://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.

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)

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

Upvotes: 3

Siz S
Siz S

Reputation: 866

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

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

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

Upvotes: 0

Ceres
Ceres

Reputation: 3648

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

Upvotes: 0

Valen
Valen

Reputation: 26

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

Upvotes: 0

Related Questions