0

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.

3 Answers 3

2

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
1

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.

1
  • 2
    8 years later, using Visual Studio 2017 having the same issue solved the same way. Hope MS will someday find a solution for it. It's really annoying. In grown applications readding controls can be a pain in the butt!
    – Luke
    Commented Jan 24, 2018 at 10:07
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.