Public Class Form1
'A dummy ParameterValue object
Dim TestValue As parameterValue = New ParameterValue With { _
.Value = 10, _
}
'Some dummy objects with populated ParameterValue objects on the BindingList
Dim TestParameters As BindingList(Of Parameters) = New BindingList(Of Parameters) From { _
New Parameter With {.UpperLimit = testValue, DefaultValue = TestValue, .LowerLimit = TestValue}, _
New Parameter With {.UpperLimit = testValue, DefaultValue = TestValue, .LowerLimit = TestValue}, _
New Parameter With {.UpperLimit = testValue, DefaultValue = TestValue, .LowerLimit = TestValue} _
}
'A dummy Profile object with a populated BindingList
Dim TestProfile As Profile = New Profile With {.Parameters = TestParameters}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set the basic data binding up
DataGridView1.AutoGenerateColumns = False
DataGridView1.Columns("UpperLimit").DataPropertyName = "UpperLimit.Value"
DataGridView1.Columns("DefaultValue").DataPropertyName = "DefaultValue.Value"
DataGridView1.Columns("LowerLimit").DataPropertyName = "LowerLimit.Value"
DataGridView1.DataSource = TestProfile .Parameters
End Sub
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
'"Fixes" the DataPropertyNames that use the dot operator before updating the value in the object and the DGV cell.
Dim Grid As DataGridView = DirectCast(sender, DataGridView)
Dim Row As DataGridViewRow = Grid.Rows(e.RowIndex)
Dim Col As DataGridViewColumn = Grid.Columns(e.ColumnIndex)
If Row.DataBoundItem IsNot Nothing AndAlso Col.DataPropertyName.Contains(".") Then 'Check if the DataPropertyName member of the current cell contains dot operators (".")
Dim PropertyStrings As String() = Col.DataPropertyName.Split("."c) 'Split the member string at the dot operators
Dim PropInfo As Reflection.PropertyInfo = Row.DataBoundItem.[GetType]().GetProperty(PropertyStrings(0)) 'Get the property that matches the first of the split strings from the type of the object bound to the current cell
Dim ParentVal As Object = Nothing
Dim Val As Object = PropInfo.GetValue(Row.DataBoundItem, Nothing) 'Get the value from the property
For i As Integer = 1 To PropertyStrings.Length - 1 'Get the property then the value of any subsequent split strings
ParentVal = Val 'Save the preceding value as the new parent object
PropInfo = Val.[GetType]().GetProperty(PropertyStrings(i)) 'Get the child as the new current property
Val = PropInfo.GetValue(Val, Nothing) ' Get the value of the new current property
Next
If (e.Value IsNot Nothing) Then
If ((e.Value <> "") And (Convert.ChangeType(e.Value, PropInfo.PropertyType) <> Val)) Then 'Update the value in the object
PropInfo.SetValue(ParentVal, Convert.ChangeType(e.Value, PropInfo.PropertyType), Nothing)
Val = PropInfo.GetValue(ParentVal, Nothing) 'Get the value back from the object (in case the getter or setter alters the value)
End If
End If
e.Value = Val 'Save the final value as the value to use in the cell
End If
End Sub
End Class