I’ve just started fooling around with the DataGridView in .NET 2.0. It’s awfully slick, but very convoluted in some ways. One can use a DataGridViewButtonColumn to display buttons in the DataGridView, which is nice.
What’s not nice about the DataGridViewButtonColumn are the silly steps you have to go through in order to get text displayed on the button. Instead of just setting the Text property, you also have to set the UseColumnTextForButtonValue boolean to true, like in the snippet below.
DataGridViewButtonColumn
checkin = new DataGridViewButtonColumn();checkin.HeaderText =
"Check In";checkin.Text =
"Here!";checkin.UseColumnTextForButtonValue =
true;checkin.Width = 45;
checkin.DisplayIndex = 0;
dgvRegistrants.Columns.Add(checkin);
Now why in the world does one have to take this extra step? If I set a button’s Text property to some non-null value, wouldn’t that generally mean I want to, well, use that value? This is completely non-intuitive. Perhaps there’s a case for this behavior around somewhere, but I didn’t run across anything when Googling around.
I was fortunate enough to hear Scott Myers speak at the Software Development Expo back in ‘03 and he was all over this kind of foolishness. “Make it hard to use your components incorrectly” was something he repeatedly said.
Thanks! This post saved me a lot of time!
ReplyDeleteMichelle Fox
Glad I was able to help!
ReplyDeleteThanks. I was wondering why no text was appearing!
ReplyDeleteThanks! It saved me a lot of time too
ReplyDeleteThanks! It saved me a lot of time too
ReplyDeleteThanks! It saved me a lot time too.
ReplyDeleteSorry, I posted my comment 3 times. Just was too happy to find this post and made sure the comment will get there.
ReplyDeleteMost helpful, seems a very counter intuitive way of doing things but at least it works. I'd tried setting the Text and using the UseColumnTextForButtonValue but hadn't used both together.
ReplyDeleteThank you very much...
ReplyDeleteIt really helped me a lot
God bless you, FrazzledDad!
ReplyDeleteGreat site! thanks for the advice.
ReplyDeleteThank you for posting this! Had no idea how to get the text to appear.
ReplyDeletefrom one Frazzled Dad to another, thanks for your post.
ReplyDeleteAnd not just thank you for the post. Thank you for denouncing such Microsoft foolishness....
How do you put an image on the button? Thanks!
ReplyDeleteThanks, I was just about to post my computer through the nearest window!!!
ReplyDeleteUnfortunately, VS is brimming with this sort of nonsense!!!
Well far be it for me to answer for MS, so I won't. BUT, surely this is just to allow you to set the SAME text for each button in the column. If you want to change it on the fly then setting this flag will prevent you from changing it.
ReplyDeleteMy thought was: how do I change other aspects of the button, such as the font, color etc?
The fact that this is the number two Google result (after the MSDN page) for DataGridViewButtonColumn speaks volumes. I, too, had textless buttons...but thanks to you, no longer! :)
ReplyDeleteThanks for that tip. Now, I too no longer have the dreaded textless datagrid column buttons. I wonder why UseColumnTextForButtonValue does not default to true, or even why it exists for that matter.
ReplyDeleteThanks too :)
ReplyDeleteI'm having the same problem but setting UseColumnTextForButtonValue to true is having no effect. Any other suggestions?
ReplyDeleteScratch that, it's working fine. My datagridview wasn't loading data yet, and the text value only seems to apply if more than one row is loaded...
ReplyDeleteThanks! From Italy.
ReplyDeleteAs a note, if you set the DataGridViewButtonColumn.Text property in the designer ('edit columns' dialog), it will not be saved in the resource file. So if your software has multi-lingual support, your developers will contantly overwrite other languages when they set this property. I had to take it out and set it in code, which isn't a big deal, but it's something everyone has to remember now.
ReplyDeletehello can i change the button text for the datagridbuttoncolumnnamne.?
ReplyDeletei have a datagridview with a button per row. the button text is Save., when a user clicks on the button of a certain row that button will change its text to Edit.? help me pls
God this was driving me nuts! I couldn't figure out why no text was appearing! Thanks a ton.
ReplyDeleteThanks.. I have created the buttons in runtime in the gridview but didn't find how to display the button text in there.
ReplyDeleteUseColumnTextForButtonValue solved this.
Three years and still pops up.
ReplyDeleteHowever, the behavior actually does make sense. If you don't set this value, the button will display the data bound to the column or blank if it isn't bound. Only if you set this flag will it ignore the data an put your Text value on.
I'd argue that since this is a DataGridView that that is the correct default behavior.
Many thanks, much appreciated.
ReplyDeleteThanks! It's 2009 and this 'feature' is still cropping up...
ReplyDeleteTime to switch to WPF?
thanks very much!
ReplyDelete