我有以下课程:
Imports Cognex.InSight
Imports Newtonsoft.Json
Public Class VariableViewModel
Public Enum VariableTypes
EditRegion
Enumerated
Input
Momentary
Toggle
End Enum
Public Property CellLocation As CvsCellLocation
Public Property Name As String
Public Property Values As Dictionary(Of String, String)
Public Property VariableType As VariableTypes
Public Function ToJson() As String
Return JsonConvert.SerializeObject(New With {Key Name, CellLocation, Values, VariableType})
End Function
End Class这个类正在序列化,最后反序列化回并存储到一个列表中。这个列表最终使用这样的一个DataGridView绑定到BindingSource:
' Private ReadOnly _variables As List(Of VariableViewModel)
Dim source As BindingSource = New BindingSource() With {
.DataSource = _variables
}
DataGridViewVariables.DataSource = source我遇到的问题是,DataGridView中的列名与类上的属性不是一对一的匹配。另外,我还想在DataGridView的末尾添加两个按钮列。
如果不清除列,然后绑定DataGridView,然后手动设置Button列,是否有方法将列名与类名匹配?
发布于 2020-06-02 04:03:14
DataPropertyName属性确定绑定到的数据源的列/属性。您可以自己在设计器或代码中将列添加到网格中,并为每个列设置该属性。在设置DataSource之前,将AutoGenerateColumns设置为False,然后只按指定的方式绑定现有的列,并且不会创建新的列。
https://stackoverflow.com/questions/62144511
复制相似问题