-
Feb 16th, 2011, 05:32 PM
#1
Thread Starter
Member
Create Custom DataGridView Cell from UserControl
Well, the title pretty much says it all. I have a user control that is a a collection of six label controls. three are used as visual color indicators, and the other three are used as the corresponding value for each color indicator. (It's sort of like a legend key on a chart or map.) I have created a custom class to store the three values, and the associated methods to interact with the values. (I can probably use a structure instead of a class, but right now, I don't want to discuss that.) My problem, is that I want to show my user control inside a cell of a DataGridView. I've looked at the examples that create a MaskedTextBox DataGridViewCell, but I can't seem to get it to work. Here is what I have:
The Column class:
Code:
Public Class DataGridViewResultsColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New DataGridViewResultsCell())
End Sub
Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As System.Windows.Forms.DataGridViewCell)
' ensure that the cell used for the template is a ResultsCell
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(DataGridViewResultsCell)) Then
Throw New InvalidCastException("Must be a DataGridViewResultsCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
The Cell class:
Code:
Public Class DataGridViewResultsCell
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
' set the value of the editing control to the current cell value
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As ResultsEditingControl = CType(DataGridView.EditingControl, ResultsEditingControl)
' use default row value when value property is null
If Not (Me.Value Is Nothing) Then
ctl.CovRes = CType(Me.Value, CoverageResult)
End If
End Sub
Public Overrides ReadOnly Property EditType() As System.Type
Get
Return GetType(ResultsEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As System.Type
Get
Return GetType(CoverageResult)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return New CoverageResult(0, 0)
End Get
End Property
End Class
I read somewhere that I need to create an editing control class to wrap my user control. I've listed that below, but I have two issues with that:
- The data contained in my UserControl is readonly. Do I really need an editing control class?
- I don't know what to code for most of the methods of the editing control class.
Code:
Public Class ResultsEditingControl
Inherits ctlCoverageResult
Implements IDataGridViewEditingControl
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
' Don't know what should go here.
End Sub
Private _DataGridView As DataGridView
Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
Get
Return _DataGridView
End Get
Set(ByVal value As System.Windows.Forms.DataGridView)
_DataGridView = value
End Set
End Property
Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
Get
' Don't know what should go here.
End Get
Set(ByVal value As Object)
' Don't know what should go here.
End Set
End Property
Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
Get
' Don't know what should go here.
End Get
Set(ByVal value As Integer)
' Don't know what should go here.
End Set
End Property
Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
Get
' Don't know what should go here.
End Get
Set(ByVal value As Boolean)
' Don't know what should go here.
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
' Don't know what should go here.
End Function
Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
' Don't know what should go here.
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
' Don't know what should go here.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
' Don't know what should go here.
End Get
End Property
End Class
After all of this, the DataGridView only displays the name of the class that I created to hold the data, i.e., [ProgramName].[ClassName]
Do I need to override the Paint method for the DataGridViewCell? If so, how do I get the cell (row and column) to resize to correctly fit my UserControl?
Thanks.
-
May 20th, 2011, 04:08 PM
#2
Frenzied Member
Re: Create Custom DataGridView Cell from UserControl
Did you ever get this to work?
Justin
-
Jun 3rd, 2011, 08:38 AM
#3
Thread Starter
Member
Re: Create Custom DataGridView Cell from UserControl
No, I was not able to get this working. I ended up using a TableLayoutPanel and mimicking the visual style of a DataGridView. For me, everything was ReadOnly, so I didn't have to emulate the functionality of the DataGridView.
-
Jun 3rd, 2011, 09:02 AM
#4
Frenzied Member
Re: Create Custom DataGridView Cell from UserControl
Yeah I was trying to make a datagridview column that hosted my custom checkbox control but never got it to work. So I just used the same checked and unchecked image I had in an image column and switched the image on click.
Thanks for the reply,
Justin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|