VS C# Checkbox replicates

I just added my first check box to a datagridview. On the screen there is a date prompt and a Display button. The user can change the date and then hit the display button and the dgv refreshes. The problem I have is which each click of the Display button the dgv shows one more check box. So the screen starts out with one, then two, three and so on. HOw do I correct this code so there will always only be one checkbox column?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You need to check if the DataGridView already has that column before trying to add it again.
Jacques Bourgeois (James Burger)PresidentCommented:
It's because you add a column every time that you refresh.

You are also redefining DataGridViewDataErrorEventHandler every time you refresh, which might lead to strange behavior if the optimizer does not remove the extra delagates.

I would work with a Form level Boolean variable so that the code for these 2 operations runs only the first time that you get into the method.

Check if that variable is False. If so, set your event handler, add your column and set the variable to True. It will be True when you come back into the method, so these line won't run again.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwheeler23Author Commented:
I discovered the column.clear() directive. If I add this to my save button just before I call the dgv no more duplicate columns.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Jacques Bourgeois (James Burger)PresidentCommented:
We, supposed "experts", often learn from the programmers who ask questions.

Some times, we have developed or learned techniques in older environments (we had something else than the DataGridView in the first versions of the framework) and still use them as an old habit, not having seen a new thing appearing between one version of Visual Studio and another.

I do not use the DataGridView anymore, but thanks for the tip. I might be able to pass it to somebody else.

If I could, I would give you the points.
rwheeler23Author Commented:
Enjoy the points guys. This language continues to drive me nuts.  Sometime is impossible to teach old dogs new tricks!
Jacques Bourgeois (James Burger)PresidentCommented:
You would have to search a while to find dogs older than I am in that field.

One of the main differences between the languages today and the languages of yesteryears is that we used to be able to learn all there was about a language in a few months of everyday use. When you compare the 60 or so commands of the old basic with the 10000+ classes that we use today, it has become impossible to know everything in a lifetime. But it is also one the main reasons I find it interesting. It never gets repetitive, there is always something new to learn to improve your work.

The DataGridView is the second worst thing I ever had to deal with in a little more than 20 years of working with controls. The worst one is the DataGrid that was provided for Windows applications in the first few first versions of .NET.

Most programmers I know who use grids often end up buying a commercial grid to replace it.
rwheeler23Author Commented:
I hear what you are saying. I started with assembly language code and actually counted clock cycles. Now if your program is too slow the solution is to buy a faster computer. If all I did was write code I would be fine but sometimes I don't touch it for six months and I forget most of what I learned.  Thanks again for your help and have a good night.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.