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

  • Pytanie

  • 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
     

    sobota, 19 sierpnia 2006 17:12

Odpowiedzi

  • 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.

    piątek, 25 sierpnia 2006 22:21

Wszystkie odpowiedzi

  • Datagridview1.AutoGenerateColumns=false
    sobota, 19 sierpnia 2006 17:36
  • 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.

     

    sobota, 19 sierpnia 2006 17:37
  • 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)?

    sobota, 19 sierpnia 2006 20:22
  • 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.

    piątek, 25 sierpnia 2006 22:21
  • 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

    czwartek, 26 kwietnia 2007 15:24
  • 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.

     

    czwartek, 26 kwietnia 2007 16:08
  • 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
    wtorek, 1 maja 2007 14:17
  • 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
    czwartek, 16 sierpnia 2007 13:33
  • 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.
    środa, 21 października 2009 12:45
  • 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;
                }
            }
        }
    }
    czwartek, 7 stycznia 2010 15:49
  • 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>
    
    • Zaproponowany jako odpowiedź przez RannyMeier czwartek, 7 stycznia 2010 17:08
    czwartek, 7 stycznia 2010 17:06
  • 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.
    • Zaproponowany jako odpowiedź przez Miral środa, 4 grudnia 2019 23:25
    piątek, 15 stycznia 2010 17:55
  • This worked perfect for me. In the constructor.

      public partial class formXYZ: Form
      {
        public formXYZ()
        {
          InitializeComponent();
          this.dataGridView1.AutoGenerateColumns = false;
        }
      }
    
    
    • Zaproponowany jako odpowiedź przez Jeison Souza czwartek, 9 czerwca 2011 14:30
    czwartek, 9 czerwca 2011 14:26
  • Thanks, its worked for me.
    sobota, 20 lipca 2013 06:37
  • 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

    czwartek, 24 października 2013 11:10
  • 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

    wtorek, 3 grudnia 2013 21:47
  • I can't Show datagridview.AutoGenerateColumns in design Time,

    Im using VS2010 sp1
    • Zmodyfikowany przez BijuKalanjoor sobota, 25 stycznia 2014 07:14
    sobota, 25 stycznia 2014 07:13
  • 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

    sobota, 6 października 2018 17:30
  • I liked your answer. Thump Up.
    wtorek, 5 lutego 2019 22:34
  • 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.

    środa, 4 grudnia 2019 23:28