Reputation: 17653
I am trying to display a custom control in a datagridview cell so that I can show nicely formatted information.
I am quite comfortable with the use of datagridview control accepting a list of objects as its datasource. I am also comfortable with populating the cell contents in the cellformatting event.
So I am planning to, instead of using a normal textbox control, button control and linkedtext control in datagridview cell, display my own custom control which is good one for displaying some custom data like, name, address, telephone, photo and others. It would be a nice improvement in my application.
I would appreciate some suggestions for other methods to achieve this.
Upvotes: 3
Views: 3993
Reputation: 9454
There's a few steps to this to neatly embed you custom user control into the DataGridView as the editing control used by a cell in that grid.
Firstly, You'll need to write a class which inherits from your custom control and implements the IDataGridViewEditingControl. This gives you a DataGridViewEditingControl class encapsulating your custom control. Call it a CustomDataGridViewEditingControl
Some of the key methods on this interfaces that you'll need to complete will be; * EditingControlFormattingValue which will need to return something that represents you value. * EditingControlWantsInputKey which will control what special key strokes you want to allow or disallow
Once you have this CustomDataGridViewEditingControl in place, encapsulating your custom control, you need to create another class, which inherits from DataGridViewTextBoxCell. Call it a CustomDataGridViewCell.
This is the class that represents the cell with your custom control in it. Set up a module level variable as an instance of your CustomDataGridViewEditingControl
private MyCustomGridViewEditingControl _editControl;
Then, override:
InitializeEditingControl - set any default values you custom control requires. You'll usually want to call the base.initialiseEditingControl first passing in the parameters received by the overridden method.
ValueType - return the data type your custom control represents e.g. return typeof(DateTime)
EditType - return the type of your editing control e.g. return typeof(MyCustomControl)
DefaultNewRowValue if you need to provide a default value to your editing control each time the DataGridView requests (inserts) a new editing row
Next you'll need yo use this overridden DataGridViewCell (CustomDataGridViewCell) class as the CellTemplate property of a column you're using. I've implemented custom DataGridViewColumns to control this in the past, but that's possibly a level further than you want/need to go. (if you're going to want to use your custom control in many grids, then you could do that.)
To do that, create a class that inherits from DataGridViewTextBoxColumn and simply set its CellTemplate property to an instance of your CustomDataGridViewCell
It's confusing because a lot of the terms and classes have similar names - hopefully this help.
In summary: 1) Create a CustomDataGridViewEditingControl - inherits from your custom user control but implements IDataGridViewEditingControl
2) Create a CustomDataGridViewCell - this uses your CustomDataGridViewEditingControl (step 1) as its editing control.
3) Use your CustomDataGridViewCell (step 2) as the CellTemplate of a DataGridViewColumn
Hope that's clear enough...
Upvotes: 3
Reputation: 44931
You need to inherit from DataGridControl and override some of the methods and properties that it produces. There are several good articles on the net the you can use for a reference, including:
How to: Host Controls in Windows Forms DataGridView Cells
Custom DataGridViewColumn & IDataGridViewEditingControl classes
Upvotes: 6