views:

363

answers:

2

I have a datagridview that is linked to a three columns in the database, and it's only displaying them (no editing). When I run the program in debug, and exit, when I return to the form in Visual Studio, it has my three columns, plus all the columns in the table it's linked to. If I don't remove these, the next time I run the program, they show up on the form. If I remove them, I have to do it every time I run the program.

Any ideas on how to fix this?

+4  A: 

Is AutoGenerateColumns set to True ?

You should set it to False if you want to prevent the DGV from creating columns (in addition to those you created manually) from the datasource.

Edit: To clarify, this admittedly weird behaviour could result if the property is not set to False in design mode itself. I'm thinking of the corner case in which you set it to False at runtime. I would also take a look at the designer file (you might have to click the "show all files" option to view it) and see what it contains pertaining to the DGV. Might be a problem there.

Alternatively, have you tried deleting the control itself and creating a new DGV with the same bindings?

Cerebrus
AutoGeneratedColumns is set to false.
Malfist
The designer file sets the AutoGeneratedColumns to false, and only shows the columns I manually added. Not the ones that get added after running the program.
Malfist
+3  A: 

According to this there is a bug in Visual Studio that requires you to set the binding source to none/null before changing it (It seems to have been around since 2005). I removed all the columns, set binding source to none, then reset the binding source to what I wanted and edited the columns. It is working now.

Malfist
Great work! Thanks for sharing this information! :-)
Cerebrus
Looks like you've found the answer and thanks for sharing the link.
lc