Tutorial :Visual Studio adds columns to DataGridView after running program



Question:

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?


Solution:1

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.


Solution:2

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?


Solution:3

After lots of digging around, this solution finally solved it for me:

It looks like the compiler is corrupting the .resx file (which contains the form's layout information, including the column settings), which means next time you load the designer it recreates all the columns from the datasource.

The fix appears to be as follows:

Do a "show all files" and then view the .resx file associated with the form and change the columns that are giving you the aberrant behavior... look for the column name followed by ".UserAddedColumn" Entry, and set it to False. In my case I actually went so far as to altogether delete the entries that say "[columnName].UserAddedColumn".


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »