[英]How to add a table control to the form?
我想在表格中添加一个表,并且该表包含2列和3行。这3行是Name,Age和Place。在此表中,我想在运行时更新第二列中相关行的值。 我想这样添加表格
例如,在上图中,名称是列1中的表行项目之一,在列2中,第一行包含名称值。
我该怎么做?
创建某种容器类(即某种集合)来存储您的键值对,然后在运行时将它们绑定到DataGrid
。
快速肮脏的例子:
Class Container
Inherits BindingList(Of Value)
Class Value
Public Property Key As String
Public Property Value As Object
End Class
Public Sub New()
Add(New Value With {.Key = "Name"})
Add(New Value With {.Key = "Age"})
Add(New Value With {.Key = "Place"})
End Sub
Default Public Overloads Property Item(ByVal key As String) As Object
Get
Return Me.FirstOrDefault(Function(v) v.Key = key)
End Get
Set(ByVal value As Object)
Dim v = Me.FirstOrDefault(Function(e) e.Key = key)
If v Is Nothing Then
Add(New Value With {.Key = key, .Value = value})
Else
v.Value = value
End If
End Set
End Property
End Class
在您的Form
:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim grd = New DataGrid With {.Dock = DockStyle.Fill}
Controls.Add(grd)
Dim container = New Container()
grd.DataSource = container
container("Age") = 12
container("Place") = "Somewhere"
End Sub
然后,您必须调整DataGrid的外观,这完全取决于您。
这样,网格便被绑定到了container
对象上,您可以轻松地读取/更改值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.