Results 1 to 6 of 6

Thread: VB.net use of GroupByGrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    VB.net use of GroupByGrid

    I have downloaded the following code and saved it in as a new class.

    http://https://social.msdn.microsoft...ws-forms-vbnet


    I then used the GroupByGrid to add a new datagridview to my form. However I want the first column to merge. The last four columns are merged in my application as in the following picture :

    Name:  DGV.jpg
Views: 565
Size:  19.5 KB



    What am I missing?
    Regards

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: VB.net use of GroupByGrid

    Nope it seems you cannot group the grid only by the values from one column.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: VB.net use of GroupByGrid

    Hi

    not sure what you really want, here a sample of group Data
    with Access

    Name:  groupdgv.jpg
Views: 424
Size:  38.7 KB


    the code...
    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim strSql As String
            strSql = "SELECT Suppliers.ContactName, Products.ProductName, "
            strSql = strSql & "Products.UnitPrice, (Select Count (*) From [Suppliers] As X "
            strSql = strSql & "Where [X].[ContactName] < [Suppliers].[ContactName]) +1 AS DummyIndex "
            strSql = strSql & "FROM Suppliers INNER JOIN Products ON Suppliers.SupplierID = Products.SupplierID "
            strSql = strSql & "ORDER BY Suppliers.ContactName;"
    
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=E:\Northwind.mdb")
            Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)
            con.Open()
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim myDataSet As DataSet = New DataSet()
    
    
            myDA.Fill(myDataSet, "MyT")
            DataGridView1.DataSource = myDataSet.Tables("MyT").DefaultView
            con.Close()
            con = Nothing
    
            ApplyFormatting()
            Me.DataGridView1.Columns(2).DefaultCellStyle.Format = "c"
            Me.DataGridView1.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
            Me.DataGridView1.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
            DGVColumsAutoSize(DataGridView1, True)
        End Sub
    
        Public Sub DGVColumsAutoSize(ByVal myDGV As DataGridView, ByVal AutoSize As Boolean)
            If myDGV Is Nothing Then
                Exit Sub
            End If
            Dim ColumnMode As DataGridViewAutoSizeColumnMode
            If AutoSize Then
                ColumnMode = DataGridViewAutoSizeColumnMode.AllCells
            Else
                ColumnMode = DataGridViewAutoSizeColumnMode.None
            End If
            With myDGV
                For i As Integer = 0 To .Columns.Count - 1
                    .Columns(i).AutoSizeMode = ColumnMode
                Next
            End With
        End Sub
    
        Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
            ApplyFormatting()
        End Sub
    
        Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
            If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then
                Using gridBrush As Brush = New SolidBrush(Me.DataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
                    Using gridLinePen As Pen = New Pen(gridBrush)
                        ' Clear cell  
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
                        ' Draw line (bottom border and right border of current cell)  
                        'If next row cell has different content, only draw bottom border line of current cell  
                        If e.RowIndex < DataGridView1.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                        End If
                        ' Draw right border line of current cell  
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
                        ' draw/fill content in current cell, and fill only one cell of multiple same cells 
                        If Not e.Value Is Nothing Then
                            If e.RowIndex > 0 AndAlso DataGridView1.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
                            Else
                                e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
                            End If
                        End If
                        e.Handled = True
                    End Using
                End Using
            End If
        End Sub
    
        Private Sub ApplyFormatting()
            For Each row As DataGridViewRow In DataGridView1.Rows
                If CInt(row.Cells(3).Value) Mod 2 = 1 Then
                    row.Cells(0).Style.BackColor = Color.LightYellow
                    row.Cells(1).Style.BackColor = Color.LightYellow
                    row.Cells(2).Style.BackColor = Color.LightYellow
                Else
                    row.Cells(0).Style.BackColor = Color.LightSteelBlue
                    row.Cells(1).Style.BackColor = Color.LightSteelBlue
                    row.Cells(2).Style.BackColor = Color.LightSteelBlue
                End If
            Next
    
            For Each row As DataGridViewRow In DataGridView1.Rows
                'color Value < 10
                Select Case CInt(row.Cells(2).Value)
                    Case Is < 10
                        row.Cells(2).Style.BackColor = Color.Aquamarine
                        'color Value > 90
                    Case Is > 90
                        row.Cells(2).Style.BackColor = Color.Pink
                End Select
            Next
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.AllowUserToAddRows = False
        End Sub
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: VB.net use of GroupByGrid

    In order to group by data, you'd have to have some common data to group by... the data in your first column look unique to me, so there's nothing to group by/merge.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: VB.net use of GroupByGrid

    Thank you Chris E - will try that.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: VB.net use of GroupByGrid

    Thank you techgnome. The data in the first column is not unique - you can only see the first bit which is unique.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width