FYI Column selection in datagridview

kageboshi

Member
Joined
Mar 8, 2009
Messages
8
Programming Experience
Beginner
Hello there. I'm new to the site.
Not too sure if I have the right prefix for this thread, but I wanted to post a bit of code that allows for an entire column to be selected in a datagridview. I spent nearly eight hours looking for this code through various search engines and when I finally found it, it was in C#; thus I had to convert it to vb.net.

So I thought I would publish this for the interweb and those that are looking for what I was looking for, a way to select an entire column in a datagridview form control in vb.net.

Here it is:

VB.NET:
        Dim row As DataGridViewRow
        For Each row In DataGrid1.Rows
            row.Cells(e.ColumnIndex).Selected = True

and I have it in the following subroutine

VB.NET:
Private Sub DataGrid1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
Handles DataGrid1.ColumnHeaderMouseClick

        Dim row As DataGridViewRow
        For Each row In DataGrid1.Rows
            row.Cells(e.ColumnIndex).Selected = True
        Next

    End Sub
 
Last edited:
You can also set SelectionMode to ColumnHeaderSelect.
 
You can also set SelectionMode to ColumnHeaderSelect.

I tried that already. Was receiving an error when I imported an xls spreadsheat using either ACE or Jet providers.

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to ColumnHeaderSelect.

With the code on my first post you were able to do a column select and it would still sort.
 
Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to ColumnHeaderSelect.
Yes, you have to configure column header either for automatic sort or selection. It would be confusing HCI to have both functionalities with same UI action, ie you can't sort without making a different selection and you can't select without making a different sort. It would be better to make up your mind what functionality clicking the column header should do, either select or sort, and do the other through a different UI action, for example a keyboard-mouse combination where Ctrl+click did the other.
 
Thank you both for the discussion. For what was required on this project, I needed the user to be able to select multiple columns. I did disable the sort as it was not a requirement. Under the datagrid1_Columnheadermousedoubleclick event I have it to deselect the column.

Where as I could have gone with what you two were talking about, I don't believe it would have fulfilled the requirements of this project. Being able to have this code allows for a more customizable datagrid in regards to what actions can be performed on it.
 
I needed the user to be able to select multiple columns.
hehe, funny you should mention it, that is the default behaviour for Ctrl-click header when you have configured DGV for ColumnHeaderSelect.
 
hehe, funny you should mention it, that is the default behaviour for Ctrl-click header when you have configured DGV for ColumnHeaderSelect.

Yeah I know...I didn't want the crtl+click. If I wasn't able to do it this way I would have prompt the user with checkboxes on which columns they needed to select.


Thanks
 
Back
Top