如何访问DataGridView中特定行的组合框的项集合?
我按如下方式填充组合:
Dim VATCombo As New DataGridViewComboBoxColumn
With VATCombo
.HeaderText = "VAT Rate"
.Name = .HeaderText
Dim VATCol As New JCVATRateCollection
VATCol.LoadAll(EntitySpaces.Interfaces.esSqlAccessType.StoredProcedure)
For Each Rate As JCVATRate In VATCol
.Items.Add(Rate.str.VATRate)
Next
.Sorted = True
VATCol = Nothing
.ToolTipText = "Select VAT Rate"
.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
.CellTemplate.Style.BackColor = Color.Honeydew
.DisplayIndex = 8
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
.Columns.Add(VATCombo)
VATCombo = Nothing
我希望能够为添加到网格的每个新行设置默认值,我还希望能够根据其他业务逻辑更改组合中的值。我意识到我可以直接设置单元格值,但我想避免将值硬编码到系统中并依赖数据库来填充。
我确信它必须是直截了当的,但这是在逃避我......
答案 0 :(得分:1)
EditGridView中基本组合框的问题是所有行共享相同的组合框,因此您无法更改它。
您要创建一个从ComboBox继承并实现IDataGridViewEditingControl的新类(ComboEditingControl):
(例如customdropdown.vb:)
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Friend Class ComboEditingControl
Inherits ComboBox
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Private ItemSelected As String
Public Sub New()
Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return ItemSelected
End Get
Set(ByVal value As Object)
If TypeOf value Is Decimal Then
Me.SelectedItem = value.ToString()
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return ItemSelected
End Function
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Up, Keys.Down, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return False
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
''' <summary>
''' Notify the DataGridView that the contents of the cell have changed.
''' </summary>
''' <param name="eventargs"></param>
''' <remarks></remarks>
Protected Overrides Sub OnSelectedIndexChanged(ByVal eventargs As EventArgs)
valueIsChanged = True
dataGridViewControl.NotifyCurrentCellDirty(True)
MyBase.OnSelectedItemChanged(eventargs)
ItemSelected = SelectedItem.ToString()
End Sub
End Class
另外2个类,请注意在InitializeEditingControl中,您需要为您所在的特定行构建项目
Friend Class ComboColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New ComboCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a ComboCell.
If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(ComboCell)) Then
Throw New InvalidCastException("Must be a ComboCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Friend Class ComboCell
Inherits DataGridViewTextBoxCell
Public Sub New()
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As ComboEditingControl = CType(DataGridView.EditingControl, ComboEditingControl)
Dim GetValueFromRowToUseForBuildingCombo As String = Me.DataGridView.Rows(rowIndex).Cells(0).Value.ToString()
ctl.Items.Clear()
For Each thing As String In ACollection
ctl.Items.Add(Sheet)
Next
If Me.Value Is Nothing Then
ctl.SelectedIndex = -1
Else
ctl.SelectedItem = Me.Value
End If
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(ComboEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(String)
End Get
End Property
Public Overrides ReadOnly Property FormattedValueType() As System.Type
Get
Return GetType(String)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return ""
End Get
End Property
End Class
最后在表单的OnLoad代码中,以编程方式将控件添加到Grid:
Dim c As New ComboColumn
c.HeaderText = "Sheet"
c.DataPropertyName = "My Combo"
c.ToolTipText = "Select something from my combo"
MyDataGridView.Columns.Add(c)
获得&amp;设置只需使用ThisRow.Cells(ComboCol).Value
第一个字段是通用的,另一个字段继承并拥有您需要的每个DataGridView的自定义代码
HTH
答案 1 :(得分:0)
您是否可以使用行索引直接在单元格中转换控件以访问当前行?