Results 1 to 4 of 4

Thread: VB How to merge cells having same data in datagridview?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    2

    VB How to merge cells having same data in datagridview?

    Hello All,

    I have a dataset in datagridview, and one row of this dataset includes a date data.
    For example,
    -----------------------
    Date | Etc.
    -----------------------
    6/24/2014 | a
    -----------------------
    6/24/2014 | a
    -----------------------
    6/25/2014 | b
    -----------------------
    6/26/2014 | c
    -----------------------
    6/26/2014 | c
    -----------------------

    I'd like to merge cells having same date. What I need to do is...
    -----------------------
    Date | Etc.
    -----------------------
    6/24/2014 | a
    -----------------------
    6/25/2014 | b
    -----------------------
    6/26/2014 | c
    -----------------------

    Any help or suggestions?
    Thanks!!

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: VB How to merge cells having same data in datagridview?

    Quote Originally Posted by tak8210 View Post
    Hello All,

    I have a dataset in datagridview, and one row of this dataset includes a date data.
    For example,
    -----------------------
    Date | Etc.
    -----------------------
    6/24/2014 | a
    -----------------------
    6/24/2014 | a
    -----------------------
    6/25/2014 | b
    -----------------------
    6/26/2014 | c
    -----------------------
    6/26/2014 | c
    -----------------------

    I'd like to merge cells having same date. What I need to do is...
    -----------------------
    Date | Etc.
    -----------------------
    6/24/2014 | a
    -----------------------
    6/25/2014 | b
    -----------------------
    6/26/2014 | c
    -----------------------

    Any help or suggestions?
    Thanks!!
    I think you are not necessarily talking about merging data, as much as removing duplicates. You can handle the underlying datatable before it is bound, here is some code below:
    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            dt.Columns.Add("ID")
            dt.Rows.Add(1)
            dt.Rows.Add(3)
            dt.Rows.Add(3)
            dt.Rows.Add(14)
            dt.Rows.Add(15)
            dt.Rows.Add(16)
            dt.Rows.Add(16)
    
            Dim dtRemovedDuplicates = RemoveDuplicateRows(dt, "ID")
            DataGridView1.DataSource = dtRemovedDuplicates
    
    
        End Sub
    
        Public Function RemoveDuplicateRows(ByVal dTable As DataTable, ByVal colName As String) As DataTable
            Dim hTable As New Hashtable()
            Dim duplicateList As New ArrayList()
    
            'Add list of all the unique item value to hashtable, which stores combination of key, value pair.
            'And add duplicate item value in arraylist.
            For Each drow__1 As DataRow In dTable.Rows
                If hTable.Contains(drow__1(colName)) Then
                    duplicateList.Add(drow__1)
                Else
                    hTable.Add(drow__1(colName), String.Empty)
                End If
            Next
    
            'Removing a list of duplicate items from datatable.
            For Each dRow__2 As DataRow In duplicateList
                dTable.Rows.Remove(dRow__2)
            Next
    
            'Datatable which contains unique records will be return as output.
            Return dTable
        End Function
    Found this function at: http://www.dotnetspider.com/resource...rom-table.aspx

    The only thing I would be wary of is if you can have multiple letters in the same day, ie:

    6/24/14 - a
    6/24/14 - b

    You would need to figure how you want to handle that scenario, but by your example this does not appear to be the case.

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB How to merge cells having same data in datagridview?

    Like this?

    Name:  111.png
Views: 6502
Size:  34.4 KB

  4. #4
    New Member
    Join Date
    May 2015
    Posts
    1

    Re: VB How to merge cells having same data in datagridview?

    @Kevininstructor, how did you worked it out?

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