Question Merge Columns in DataGridView

ksyong17

New member
Joined
Aug 5, 2008
Messages
2
Programming Experience
1-3
Hi There,

I'm currently building an vb.net application which is similiar to Excel where I need to merge 2 Columns together. I'm using Datagridview to populate the data. I need the data to be populated in such a way that one column would carry 2 sets of data.

Any idea of how to approach this?

Thanks in advance

ksyong17
 
Merge columns in a DataGridView as in Excel SpreadSheet

Article is in C# so I ran it through a converter. Will probably need to tweak it a bit.

In form load:

VB.NET:
Dim d As DataGridViewColumn
For Each d In dataGridView1.Columns
        d.SortMode = DataGridViewColumnSortMode.Programmatic
Next
dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect

VB.NET:
Dim Newcol As DataGridViewColumn =  New DataGridViewColumn() 
Newcol.HeaderText = "Merged Column"
Newcol.Name = "Merged Column"
Newcol.CellTemplate = dataGridView1.SelectedCells(0)
Dim o As Object = New Object() 
Newcol.ValueType = o.GetType()
dataGridView1.Columns.Add(Newcol)
Dim mergedValue As String
Dim r As DataGridViewRow
For Each r In dataGridView1.Rows
       mergedValue = ""
       Dim box As DataGridViewCell
       For Each box In r.Cells
              If box.Value Is Nothing Then
                     box.Value = ""
              End If
              mergedValue = mergedValue + " " + box.Value.ToString()
       Next
 
       r.Cells("Merged Column").Value = mergedValue
Next
 
Back
Top