I thought I'd share my workaround for the above problem in case any one else was trying to accomplish the same thing. I just turn off AutoGenerateColumns
and create my own columns by looping through the column collection in the DataTable. It's important to clear the data source and columns each time in case they differ from call to call. Other than that, I just set the col name, header, and data property from the DataTable column and we're good to go.
''' <summary>
''' Generates DataGridViewTextBox Columns for every field of data in the data source
''' </summary>
''' <param name="dgv">The DataGridView having columns and data generated</param>
''' <param name="dt">The source data as a DataTable</param>
''' <remarks></remarks>
Private Sub AutoGenerateTextBoxColumns(ByVal dgv As DataGridView, ByVal dt As DataTable)
'declare local variables
Dim column As DataGridViewTextBoxColumn
'first clear datasource and columns
If Not IsNothing(dgv.DataSource) Then dgv.DataSource.Clear()
'clear way for potentially different columns
If Not IsNothing(dgv.Columns) Then dgv.Columns.Clear()
'set autogenerate columns to false
dgv.AutoGenerateColumns = False
'generate columns for DataGridView
For Each col As DataColumn In dt.Columns
column = New DataGridViewTextBoxColumn()
With column
.HeaderText = col.ColumnName
.DataPropertyName = col.ColumnName
.Name = col.ColumnName
End With
dgv.Columns.Add(column)
Next
'add datatable as datasource
dgv.DataSource = dt
End Sub