How Can I Create My Own DataGridColumn Type?

Apr 16, 2009 at 1:44 PM
I've just started using the DataGrid and wish to create my own column type rather than using the templated column.

I have my own control called NumericTextBox, which (as you probably guessed) only accepts numeric values.

I've already tried using the templated column type with my NumericTextBox, but the functionality is a little clunky - in order to edit a numeric value you have to double-click to get the cell into edit mode then click again to give the NumericTextBox focus and display the caret ready for editing.  Another thing that's missing is the ColumnHeaderStyle, when you use a templated column you have to create your own style if you want to highlight the column headers when hovering over them, you also lose the ability to sort by default - this all has to be manually implemented.

My plan is to create a DataGridNumericTextColumn that is basically a copy of the standard DataGridTextColumn, only with a NumericTextBox instead of a standard TextBox.  Obviously I want to keep this code within my own solution rather than incorporate it into the Microsoft source code, but since it needs access to a lot of the internal objects/methods I'm not sure about the best way to achieve this.

Has anyone else managed to do anything similar?

Apr 21, 2009 at 8:38 AM
Have you tried using a stock DataGridTextColumn and overriding EditingElementStyle (I think that's the name of the property) making it contain an instance of your NumericTextBox rather than a standard TextBox? If I am not misunderstanding what you are trying to do, that should work. If so, you could subclass DataGridTextColumn and simply override the default value of the property in the constructor.
Apr 21, 2009 at 8:57 AM
Thanks mikj, I hadn't tried that.

I'll post back later to let you know if it worked  :-)
Apr 21, 2009 at 12:13 PM
If you subclass DataGridTextColumn you'll want to override GenerateElement and GenerateEditingElement.
Apr 27, 2009 at 8:54 PM
I was able to create a custom DataGridColumn type by inheriting from DataGridBoundColumn and overriding the GenerateElement, GenerateEditingElement, and CommitCellEdit methods.  I also override the PrepareCellForEdit method to get text to be selected when edit mode is entered.

I wanted text input to automatically start an edit of the current cell.  This is done in DataGridTextColumn by overriding the OnInput method.  However, this method is currently set as internal, and so I could not override it in my own assembly.  Is it possible for a future release of the toolkit to change the OnInput and BeginEdit methods to protected instead of internal?

Jun 1, 2009 at 2:40 PM

I also would like to switch to edit mode when the user types in the cell. But like dgish, the OnInput method and BeginEdit methods are internal so those are no help. Is there another way to accomplish this?

Mar 21, 2011 at 8:58 AM

Bump. Is it just me or does it seem like there are a lot of arbitrary "Internal" methods where there is no valid reason to be internal... why is a virtual method internal of a non internal class? That's ridiculous.