VB2008 datagridview multiselect and merge columns

Steve36445

Well-known member
Joined
Dec 23, 2007
Messages
58
Programming Experience
10+
Can you help me please?

vb2008

I have an unbound datagridview.

I would like

1) the user to be able to select multiple columns by clicking on the column headers ( and say " shift").

2) Concatenate the columns
e g " Fred" and " Bloggs" becomes "Fred Bloggs".

Could you please show me how to set up the dgv and Concatenate the columns?

Thanks,

Steve
 
1: Select ColumnHeaderSelect as SelectionMode for the DGV. (SortMode can't be set to Automatic for the columns)

2: Try the code below, the idea here is that the user selects the columns and then click a button or menu to adds all the selected columns into first column and remove the others. For the merge all column data is treated as strings.
VB.NET:
Private Sub MergeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MergeButton.Click
    'get indexes
    Dim columnIndexes = From col In Me.DataGridView1.SelectedColumns.Cast(Of DataGridViewColumn)() _
                        Order By col.DisplayIndex _
                        Select col.Index
    Dim mergeColumnIndex = columnIndexes(0)

    'merge header texts        
    Dim headers = From ix In columnIndexes Select Me.DataGridView1.Columns(ix).HeaderText
    Me.DataGridView1.Columns(mergeColumnIndex).HeaderText = String.Join(" ", headers.ToArray)

    'merge rows (as strings)
    For Each r As DataGridViewRow In Me.DataGridView1.Rows
        Dim row As DataGridViewRow = r '(resolves 'iteration variable in query' warning)
        Dim values = From ix In columnIndexes Select CStr(row.Cells(ix).Value)
        row.Cells(mergeColumnIndex).Value = String.Join(" ", values.ToArray)
    Next

    'remove all but first selected column
    For ix As Integer = columnIndexes.Count - 1 To 1 Step -1
        Me.DataGridView1.Columns.RemoveAt(columnIndexes(ix))
    Next
End Sub
 
Thanks John,

I am still having rpoblems configuring the dgv.

when I try to set the number of columns with
VB.NET:
   DataGridView1.ColumnCount = max_cols

I get the following error-

System.InvalidOperationException was unhandled
Message="Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to ColumnHeaderSelect."
Source="System.Windows.Forms"
StackTrace:



1: Select ColumnHeaderSelect as SelectionMode for the DGV. (SortMode can't be set to Automatic for the columns

I can't find the "ColumnHeaderSelect" property.

Thanks again,

Steve
 
The default Column's SortMode is Automatic, you have to set a different value to allow column header select, else clicking header defaults to automatic sort.

ColumnHeaderSelect: select the control in designer and find the SelectionMode property in Properties window, here you can select the ColumnHeaderSelect value.

You can of course set properties in code also.
 
Thanks John,

I've done part 1, set the sortmode to "programatic"

Do you think you could tell me howI can get multipe columns to be selected when I click on the headers?

Thanks,

Steve
 
I told you, set SelectionMode property to ColumnHeaderSelect, default is to hold Shift key when clicking them (as you also said yourself in first post) or Control key.
 
Thanks, John.

I think I mucked it up when I tried to add colums progromatically usin-

DataGridView1.ColumnCount = max_cols


If it helps others eventually I got around it by Deleting the dgv and starting again adding a new one and (hopefully) doing as you suggeseted
all in the form designer window-


1. Checking the DGV.sortmode was set to “RowHeaderSelect” (default mode)*
2. Adding the columns through the columns property.
3. Editing the columns (also through columns property in the designer window) changing the columns sort mode to “programmatic”
4. Changing the dgv sortmode to “ColumnHeaderSelect”


I was trying to do it programmatically and failed dismally!


Your second suggestion worked beautifully

Thanks again.


Steve

ps.I think the problem might have been I changed the sortmode before adding the columns.
 
Last edited:
ps.I think the problem might have been I changed the sortmode before adding the columns.
No, opposite (you meant SelectionMode of course). You can't have SortMode:automatic for any column when setting SelectionMode to column header select. There is no inherent difference in doing this in designer and in code, but obviously you can't set ColumnCount in designer. The code is really simple (though I understand this is all new for you :p):
VB.NET:
Me.DataGridView1.ColumnCount = 10

For Each col As DataGridViewColumn In Me.DataGridView1.Columns
    col.SortMode = DataGridViewColumnSortMode.NotSortable
Next

Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
 
Rows

Hi there

is it possible to do this exact thing, but to merge rows rather than columns?

thanks! :)

{{
ps. IM using JohnH's first reply:


Private Sub MergeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MergeButton.Click 'get indexes Dim columnIndexes = From col In Me.DataGridView1.SelectedColumns.Cast(Of DataGridViewColumn)() _ Order By col.DisplayIndex _ Select col.Index Dim mergeColumnIndex = columnIndexes(0) 'merge header texts Dim headers = From ix In columnIndexes Select Me.DataGridView1.Columns(ix).HeaderText Me.DataGridView1.Columns(mergeColumnIndex).HeaderText = String.Join(" ", headers.ToArray) 'merge rows (as strings) For Each r As DataGridViewRow In Me.DataGridView1.Rows Dim row As DataGridViewRow = r '(resolves 'iteration variable in query' warning) Dim values = From ix In columnIndexes Select CStr(row.Cells(ix).Value) row.Cells(mergeColumnIndex).Value = String.Join(" ", values.ToArray) Next 'remove all but first selected column For ix As Integer = columnIndexes.Count - 1 To 1 Step -1 Me.DataGridView1.Columns.RemoveAt(columnIndexes(ix)) NextEnd Sub
 
Back
Top