我有一个datagridview,其数据源包含多对多的关系 Bin {ID,Name,BinGradeID},BinGradeID {BinID,GradeID,GradeName},等级{ID,Name}
我正在尝试在CheckedListBoxColumn中公开Grades,现在我似乎无法将任何内容放入checkedlistboxcolumn
我已经以编程方式将CheckedListBoxColumn添加到DataGridView。 我不知道如何将其绑定到数据源,我在这里尝试了两个示例Add multiple checkedListbox to columns of datagrid View
我已经尝试了杰里米·汤普森发布的两个示例代码 - 我希望他的例子看起来不错,但我没有得到任何像这样的弹出/输出:
我使用checkedlistboxcolumn尝试了另一个,但这也无效。
以下是我将Column添加到DataGridView
的代码Dim dgvChkCol As CheckedListBoxColumn = New CheckedListBoxColumn()
dgvChkCol.DataPropertyName = "GradeID"
dgvChkCol.Name = "GradesCheckBox"
dgvChkCol.Visible = True
dgvChkCol.HeaderText = "GradesCheckBox"
DataGridView1.Columns.Add(dgvChkCol)
VB或C#中的任何代码都适合我 - 我可以轻松转换 - 我只是缺少CheckedListBox以及如何在DataGridView中执行此操作
如果有人想查看我的checkedlistbox代码 - 这是C#转换为VBNET
Public Class CheckedListBoxColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New CheckedListBoxCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(value As DataGridViewCell)
If value IsNot Nothing AndAlso Not value.[GetType]().IsAssignableFrom(GetType(CheckedListBoxCell)) Then
Throw New InvalidCastException("Must be a CheckedListBoxCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class CheckedListBoxCell
Inherits DataGridViewCell
Public Sub New()
MyBase.New()
End Sub
Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As CheckedListBoxEditingControl = TryCast(DataGridView.EditingControl, CheckedListBoxEditingControl)
InitializeCheckedListBox(ctl, DirectCast(Me.FormattedValue, ICollection))
End Sub
Private Sub InitializeCheckedListBox(ctrl As CheckedListBox, value As ICollection)
ctrl.Items.Clear()
For Each obj As Object In value
ctrl.Items.Add(obj.ToString())
Next
ctrl.Tag = Me.Value
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(CheckedListBoxEditingControl)
End Get
End Property
Protected Overrides Function GetFormattedValue(value As Object, rowIndex As Integer, ByRef cellStyle As DataGridViewCellStyle, valueTypeConverter As System.ComponentModel.TypeConverter, formattedValueTypeConverter As System.ComponentModel.TypeConverter, context As DataGridViewDataErrorContexts) As Object
If value Is Nothing Then
Return New List(Of Object)()
End If
Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
End Function
Public Overrides ReadOnly Property FormattedValueType() As Type
Get
Return GetType(ICollection)
End Get
End Property
Public Overrides Property ValueType() As Type
Get
Return GetType(ICollection)
End Get
Set(value As Type)
End Set
End Property
Private internalControl As CheckedListBox
Protected Overrides Sub Paint(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, cellBounds As System.Drawing.Rectangle, rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, _
formattedValue As Object, errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle, paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, _
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
graphics.FillRectangle(New SolidBrush(cellStyle.BackColor), cellBounds)
If internalControl Is Nothing Then
internalControl = New CheckedListBox()
End If
internalControl.Items.Clear()
Dim collection As ICollection = TryCast(value, ICollection)
If collection IsNot Nothing Then
For Each obj As Object In collection
internalControl.Items.Add(obj)
Next
Dim bmp As New Bitmap(cellBounds.Width, cellBounds.Height)
internalControl.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
graphics.DrawImage(bmp, cellBounds, New Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel)
End If
End Sub
Protected Overrides Sub OnClick(e As DataGridViewCellEventArgs)
Me.DataGridView.BeginEdit(False)
MyBase.OnClick(e)
End Sub
Public ReadOnly Property CheckedItems() As ICollection 'Implements IDataGridViewEditingControl.
Get
If internalControl Is Nothing Then
Return Nothing
Else
If internalControl IsNot Nothing Then
If internalControl.CheckedItems.Count > 0 Then
Return internalControl.CheckedItems
Else
If DirectCast(Me.dataGridView.Rows(rowIndex).Cells(ColumnIndex).DataGridView.EditingControl, CheckedListBox) IsNot Nothing Then
Dim checks As CheckedListBox = DirectCast(Me.dataGridView.Rows(rowIndex).Cells(ColumnIndex).DataGridView.EditingControl, CheckedListBox)
If checks.CheckedItems.Count > 0 Then
Return checks.CheckedItems
Else
Return Nothing
End If
Else
Return Nothing
End If
End If
Else
Return Nothing
End If
End If
End Get
End Property
End Class
Class CheckedListBoxEditingControl
Inherits CheckedListBox
Implements IDataGridViewEditingControl
Private dataGridView As DataGridView
Private valueChanged As Boolean = False
Private rowIndex As Integer
Public Sub New()
End Sub
' Implements the IDataGridViewEditingControl.EditingControlFormattedValue
' property.
Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Tag
End Get
' this.Tag = value;
Set(value As Object)
End Set
End Property
' Implements the
' IDataGridViewEditingControl.GetEditingControlFormattedValue method.
Public Function GetEditingControlFormattedValue(context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return EditingControlFormattedValue
End Function
' Implements the
' IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
Public Sub ApplyCellStyleToEditingControl(dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
' Implements the IDataGridViewEditingControl.EditingControlRowIndex
' property.
Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndex
End Get
Set(value As Integer)
rowIndex = value
End Set
End Property
' Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
' method.
Public Function EditingControlWantsInputKey(key As Keys, dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.[End], _
Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function
' Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
' method.
Public Sub PrepareEditingControlForEdit(selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
' Implements the IDataGridViewEditingControl
' .RepositionEditingControlOnValueChange property.
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
' Implements the IDataGridViewEditingControl
' .EditingControlDataGridView property.
Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridView
End Get
Set(value As DataGridView)
dataGridView = value
End Set
End Property
' Implements the IDataGridViewEditingControl
' .EditingControlValueChanged property.
Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueChanged
End Get
Set(value As Boolean)
valueChanged = value
End Set
End Property
' Implements the IDataGridViewEditingControl
' .EditingPanelCursor property.
Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
End Class