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!!
Re: VB How to merge cells having same data in datagridview?
Quote:
Originally Posted by
tak8210
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.
1 Attachment(s)
Re: VB How to merge cells having same data in datagridview?
Re: VB How to merge cells having same data in datagridview?
@Kevininstructor, how did you worked it out?