chris.au
chris.au

Reputation: 1108

DataGridView columns still automatically created even with AutoGenerateColumns = False

I have a DataGridView and have the AutoGenerateColumns property set to false, but the when I build my project the columns are Auto Generated.

I can see the property set to false in the Designer.vb code for the Form.

I've had this problem before and I'm not sure how to fix it.

Any advise would be greatly appreciated.

Thanks.

Upvotes: 0

Views: 3689

Answers (3)

Ray Held
Ray Held

Reputation: 1

I have found that if you're trying to setup columns during load of the form, then you expierence weird issues like this. Instead, just before populating the grid, I check to see if there are columns defined and if not, then I go ahead and configure the columns at that point. This is working consistenly for me -- when populating the columns, I set the property to auto add columns = false first thing:

    Private Sub Populate_dgvQuoteSelection(status_id As Int32)
    dgvQuoteSelection.DataBindings.Clear()
    If dgvQuoteSelection.Columns.Count = 0 Then
        Setup_dgvQuoteSelection()
    End If
    Try
        dgvQuoteSelection.DataSource = DataService.Quote_HeaderDataService.Quote_GetListView_byStatus(status_id)
    Catch ex As Exception
        MessageBox.Show(String.Format("An error occured while trying to get the grid data: {0}", ex.Message), "Error populating grid", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Upvotes: 0

RamNow
RamNow

Reputation: 486

I know this is an old question, but maybe it helps someone who searches for the same issue:

I ran in this problem today. It is important to set the 'AutoGenerateColumns'-property before you set the DataSource, otherwise the columns get generated before you told the DataGridView not to do so:

DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = mySource

Upvotes: 2

chris.au
chris.au

Reputation: 1108

I have re-added the control and it seems to be working for the time being. I believe something became corrupted, causing the problem. As I mentioned in the question I've had this happen before. If anyone else has a problem like this it would be great if you could provide some details.

Upvotes: 1

Related Questions