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?
DataGridCode.txt
LVL 1
rwheeler23Asked:
Who is Participating?
 
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.
0
 
Bob LearnedCommented:
You need to check if the DataGridView already has that column before trying to add it again.
0
 
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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.
0
 
rwheeler23Author Commented:
Enjoy the points guys. This language continues to drive me nuts.  Sometime is impossible to teach old dogs new tricks!
0
 
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.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.