I'm trying to create a custom column in a DataGridView by sub-classing the DataGridViewColumn and DataGridViewTextBoxCell classes. Can anyone please help me with a few issues that I've encountered?
I'm relatively new to this so started by replicating a VB/C# example I found online. But even when I reduce this to it's most basic - a column and cell which just replicates the standard behaviour I still hit issues.
My code looks like this. The new cell:
CLASS DataGridViewBarGraphCell INHERITS System.Windows.Forms.DataGridViewCell : METHOD OVERRIDE PROTECTED VOID Paint( INPUT graphics AS System.Drawing.Graphics, INPUT clipBounds AS System.Drawing.Rectangle, INPUT cellBounds AS System.Drawing.Rectangle, INPUT rowIndex AS INTEGER, INPUT cellState AS System.Windows.Forms.DataGridViewElementStates, INPUT value0 AS System.Object, INPUT formattedValue AS System.Object, INPUT errorText AS CHARACTER, INPUT cellStyle AS System.Windows.Forms.DataGridViewCellStyle, INPUT advancedBorderStyle AS System.Windows.Forms.DataGridViewAdvancedBorderStyle, INPUT paintParts AS System.Windows.Forms.DataGridViewPaintParts ): MESSAGE "In Paint" VIEW-AS ALERT-BOX. SUPER:Paint(INPUT graphics, INPUT clipBounds, INPUT cellBounds, INPUT rowIndex, INPUT cellState, INPUT value0, INPUT formattedValue, INPUT errorText, INPUT cellStyle, INPUT advancedBorderStyle, INPUT paintParts). RETURN. END METHOD. END CLASS.
The new column:
CLASS DataGridViewBarGraphColumn INHERITS DataGridViewColumn:
CONSTRUCTOR PUBLIC DataGridViewBarGraphColumn ( ): SUPER (). THIS-OBJECT:CellTemplate = NEW DataGridViewBarGraphCell(). THIS-OBJECT:ReadOnly = TRUE. END CONSTRUCTOR. END CLASS.
I then have a simple form with a DataGridView connected to a datasource.
My issues are:
1) I can't consistently see the new column type in the DataGridView designer. It did appear on one occasion but I can't get it back.
2) If I manually change the column type (not ideal), adding one to the grid fails in InitializeComponent() at:
with error - System.InvalidOperationException: Column cannot be added because its CellType property is null.
This doesn't seem to be the case. If I add each column individually using DataGridView:Add it doesn't complain.
3) When I run it up now, all the standard columns display correctly. But for my custom column I get this error:
A .NET object is attempting to access a method or property of an ABL/.NET hybrid class that has already been DELETED (14791)
It never gets as far as the message in the Paint method. If I remove the Paint override method it all works as expected (standard text cell behaviour).
Any help with these problem would be greatly appreciated.
Has nobody had any experience with this? Or am I going about what I'm trying to do in completely the wrong way?
What is it that you wish to achieve with this custom column?
At the moment I'm doing some investigation into what is possible. With this particular example I was trying to paint a portion of the cell based on a value and also include some text.
As I said, this was based upon a VB/C# example/tutorial I found here:
I've stripped it down to the bare essentials to just get it to perform as a standard DataGridViewTextBoxCell, but hit the issues above.
Ultimately, this is the type of thing we will be looking to do - custom cells with a mixture of graphical and text elements.