locked
How to turn off "AutoGenerate" of columns when a DataGridView is data-bound RRS feed

  • Question

  • I have a Windows form that has a DataGridView on it.  I have bound the DataGridView to show data, that I specifed,  from the database (I defined the cells that I want to see through the property page).  But, the DataGridView is showing the cells I defined AND the data being returned from the dataset.

    So, the question - what property do I set to turn off the default columns being returned from the dataset?  With a web app, there is an "AutoGenerate Columns" that you set to true or false.  What's the WinForm equivalent?

    Thanks,

    Dexter
     

    Saturday, August 19, 2006 5:12 PM

Answers

  • I just had the same problem and it was because I was setting the datasource before I set the autogeneratecolumns to false. So I think it was binding, getting the data, then setting it to false.

     

    Ex:

    // FAILED
    datagridview.DataSource = mydatasource;
    datagridview.AutoGenerateColumns = false;

    // WORKED
    datagridview.AutoGenerateColumns = false;
    datagridview.DataSource = mydatasource;

    Not sure if that helps, but at least something to check.

    Friday, August 25, 2006 10:21 PM

All replies

  • Datagridview1.AutoGenerateColumns=false
    Saturday, August 19, 2006 5:36 PM
  • just to add.... There are no "default" columns in the dataset, it will return the columns defined in your table in the database, along with the results.

     

    Saturday, August 19, 2006 5:37 PM
  • that's what I thought but "extra" columns are still being displayed.

    Example:  Through the property pages, I defined the "Columns" property to display name and address.  However, the resultset that I bound the DataGridView with also had zip and state.  Well, the result after I bound the control was that name and address are displayed (as I requested).  However, zip and state (and whatever else is in the table).

    Speaking of table - if I were actually getting data from a table via a strored procedure, I could simply not specify the unwanted columns in my query.  In this case however, I am binding the DataGridView control with an arraylist of objects that has the data that I need.

    Could this be some subtle interaction with databinding and arrays (as opposed to Datasets)?

    Saturday, August 19, 2006 8:22 PM
  • I just had the same problem and it was because I was setting the datasource before I set the autogeneratecolumns to false. So I think it was binding, getting the data, then setting it to false.

     

    Ex:

    // FAILED
    datagridview.DataSource = mydatasource;
    datagridview.AutoGenerateColumns = false;

    // WORKED
    datagridview.AutoGenerateColumns = false;
    datagridview.DataSource = mydatasource;

    Not sure if that helps, but at least something to check.

    Friday, August 25, 2006 10:21 PM
  • Sorry to revive an old post but it was relevant to my question

     

    I to have disabled auto generate programatically but I distinctly remember this being a property I could set at design time in the designer, where has that feature gone?

     

    I am using VS2005 and .Net 2.0 and the DataGridView.

     

    Cheers

    Thursday, April 26, 2007 3:24 PM
  • In the Properties tab, select the 'Columns' collection dropdown.  A dialog box pops up with fields that you can add to your gridview.  There is an 'Auto-generate fields' checkbox at the bottom of this dialog box.

     

    Thursday, April 26, 2007 4:08 PM
  • Well I have seen that during web development I agree and should point out then I am currently working on a windows forms application and this tick box does not appear to be present anywhere I look for it. Maybe cause it isn't there of course Smile
    Tuesday, May 1, 2007 2:17 PM
  • I am using Visual Studio 2005 and also am unable to find the "AutoGenerateColumns" property anywhere in the designer for a DataGridView component.  Not in the likely places I would expect, in the "Appearance", "Behavior", or "Layout", nor when sort alphabetically.  It also does not appear as a checkbox in the column collection dialog box as mentioned above.

    I set it in the form's OnLoad handler, before any data binding occurs.

    Anjo
    Thursday, August 16, 2007 1:33 PM
  • Seems like in the past it's been the designer properties and in the Edit Columns dialog. Not it's in neither. And it's now October 2009 and I'm using VS08.

    It does appear that auto generated columns are added to the collection too now so it may have been there thinking that you can just remove whatever columns you want but the real bug is that the designer will add them all back in again so you are back to doing everything programmatically anyway.
    Wednesday, October 21, 2009 12:45 PM
  • I tried making this AutoGenerateColumns virus go away by sprinkling some AutoGenerateColumns =  false in places and using a new class and still no luck.  I guess trying to hide it does not help.  The property is not virtual.  I am not smart enough to be able to imagine why this problem was invented.
    namespace DGV
    {
        public class DGVnoAGC : DataGridView
        {
            public DGVnoAGC()
            {
                base.AutoGenerateColumns = false;
            }
            public new bool AutoGenerateColumns
            {
                get
                {
                    return false;
                }
            }
        }
    }
    Thursday, January 7, 2010 3:49 PM
  • ANSWER that worked for me!  There were a bunch of these .UserAddedColumn metadata in the .resx file for the form. Getting rid of those makes the DataGridView.AutoGenerateColumns virus stop happening!  None of the setting the AutoGenerateColumns = false or trying to subclass the DGV did any good.  The VS Designer was too persistant for any of those attempts.
      <metadata name="ID.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <value>True</value>
      </metadata>
    
    • Proposed as answer by RannyMeier Thursday, January 7, 2010 5:08 PM
    Thursday, January 7, 2010 5:06 PM
  • Also worked for me. A simpler, and maybe safer way to remove the metadata statements is to open the .resx file using the VS resource editor. Select 'Other' from the dropdown box (Strings, Images, Icon ....) and delete items with the '.UserAddedColumn' in the name. It Seems like deleting these would have the opposite effect of telling the designer to auto generate the columns, but whatever works.

    'AutoGenerateColumns' option does not appear in the properties dialog or 'Columns Collection' for me either (VS2005). But it does appear to be set to 'false' by default in the 'InitializeComponent' member of the .Designer.cs file for what good it does in this case.
    • Proposed as answer by Miral Wednesday, December 4, 2019 11:25 PM
    Friday, January 15, 2010 5:55 PM
  • This worked perfect for me. In the constructor.

      public partial class formXYZ: Form
      {
        public formXYZ()
        {
          InitializeComponent();
          this.dataGridView1.AutoGenerateColumns = false;
        }
      }
    
    
    • Proposed as answer by Jeison Souza Thursday, June 9, 2011 2:30 PM
    Thursday, June 9, 2011 2:26 PM
  • Thanks, its worked for me.
    Saturday, July 20, 2013 6:37 AM
  • Old post - but still and issue in VS 2012. Thanks :)

    Please mark posts as answers/helpful if it answers your question/or helped you solve your problem.
    MCTS: .NET Framework 4, Service Communication Applications

    Thursday, October 24, 2013 11:10 AM
  • water resources,

    Thanks, using the resource manager worked and had never know about the "other" category or viewing .resx files for forms/windows in that manner.

    Interestingly in my case, and just wondering if there is something to the fact that RannyMeier had a column name "ID". . . .you see.. .although there were numerous columns being autogenerated, when I removed only the line in the .resx file that was for a column named "id", all columns stopped being autogenerated.

    Also, like another poster mentioned, this topic is still relevant it seems as my issue was occurring in vs 2010.

    Kent

    Tuesday, December 3, 2013 9:47 PM
  • I can't Show datagridview.AutoGenerateColumns in design Time,

    Im using VS2010 sp1
    Saturday, January 25, 2014 7:13 AM
  • I can’t find the AutoGenerateColumns property anywhere, but I fund a simple workaround. The columns I want to show added in design time as «Unbound columns». The autogenerated columns I don’t want is Databound columns. So I added thus code in the ColumnAdded event handler:

    Private Sub gvWorkOrders_ColumnAdded(sender As Object, e As DataGridViewColumnEventArgs) Handles gvWorkOrders.ColumnAdded

    If e.Column.IsDataBound Then e.Column.Visible = False

    End Sub

    Saturday, October 6, 2018 5:30 PM
  • I liked your answer. Thump Up.
    Tuesday, February 5, 2019 10:34 PM
  • Also worked for me. A simpler, and maybe safer way to remove the metadata statements is to open the .resx file using the VS resource editor. Select 'Other' from the dropdown box (Strings, Images, Icon ....) and delete items with the '.UserAddedColumn' in the name. It Seems like deleting these would have the opposite effect of telling the designer to auto generate the columns, but whatever works.

    This is the only thing that worked for me.  AutoGenerateColumns was already set to false by the designer, but it was still stubbornly auto-generating the columns anyway until I removed all the UserAddedColumn values from the resources.

    Wednesday, December 4, 2019 11:28 PM