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:

  1. Thanks! This post saved me a lot of time!

    Michelle Fox

    ReplyDelete
  2. Thanks. I was wondering why no text was appearing!

    ReplyDelete
  3. Thanks! It saved me a lot of time too

    ReplyDelete
  4. Thanks! It saved me a lot of time too

    ReplyDelete
  5. Thanks! It saved me a lot time too.

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

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

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

    ReplyDelete
  9. God bless you, FrazzledDad!

    ReplyDelete
  10. Great site! thanks for the advice.

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

    ReplyDelete
  12. 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....

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

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

    ReplyDelete
  15. 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?

    ReplyDelete
  16. 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! :)

    ReplyDelete
  17. 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.

    ReplyDelete
  18. Thanks too :)

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

    ReplyDelete
  20. 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...

    ReplyDelete
  21. Thanks! From Italy.

    ReplyDelete
  22. 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.

    ReplyDelete
  23. 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

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

    ReplyDelete
  25. 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.

    ReplyDelete
  26. 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.

    ReplyDelete
  27. Many thanks, much appreciated.

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

    ReplyDelete
  29. thanks very much!

    ReplyDelete