Saturday, January 14, 2006

DataGridViewButtonColumn.Text Silliness

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.

30 comments:

Anonymous said...

Thanks! This post saved me a lot of time!

Michelle Fox

Jim Holmes said...

Glad I was able to help!

Luke said...

Thanks. I was wondering why no text was appearing!

Anonymous said...

Thanks! It saved me a lot of time too

nat said...

Thanks! It saved me a lot of time too

nat said...

Thanks! It saved me a lot time too.

nat said...

Sorry, I posted my comment 3 times. Just was too happy to find this post and made sure the comment will get there.

Anonymous said...

Most 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.

Samir M. El-Mokadem said...

Thank you very much...
It really helped me a lot

Anonymous said...

God bless you, FrazzledDad!

Anonymous said...

Great site! thanks for the advice.

Peter said...

Thank you for posting this! Had no idea how to get the text to appear.

VBUG Team said...

from one Frazzled Dad to another, thanks for your post.

And not just thank you for the post. Thank you for denouncing such Microsoft foolishness....

Anonymous said...

How do you put an image on the button? Thanks!

Graham Ball said...

Thanks, I was just about to post my computer through the nearest window!!!
Unfortunately, VS is brimming with this sort of nonsense!!!

Simon Heffer said...

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.

My thought was: how do I change other aspects of the button, such as the font, color etc?

Kyralessa said...

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! :)

Anonymous said...

Thanks 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.

Anonymous said...

Thanks too :)

Randy Rue said...

I'm having the same problem but setting UseColumnTextForButtonValue to true is having no effect. Any other suggestions?

Randy Rue said...

Scratch 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...

Anonymous said...

Thanks! From Italy.

Vince said...

As 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.

Anonymous said...

hello can i change the button text for the datagridbuttoncolumnnamne.?
i 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

Justin said...

God this was driving me nuts! I couldn't figure out why no text was appearing! Thanks a ton.

Foyzul Karim said...

Thanks.. I have created the buttons in runtime in the gridview but didn't find how to display the button text in there.
UseColumnTextForButtonValue solved this.

Anonymous said...

Three years and still pops up.

However, 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.

Anonymous said...

Many thanks, much appreciated.

Anonymous said...

Thanks! It's 2009 and this 'feature' is still cropping up...
Time to switch to WPF?

Anonymous said...

thanks very much!

Subscribe (RSS)

The Leadership Journey