将 My.Settings 绑定到 datagridview 以便最终用户可以修改的最佳方法?
我们如何将 datagridview 数据绑定到某些 My.Settings(用户范围)属性,以便用户可以编辑值?这必须是 datagridview。我知道我们可以以带有文本框等的形式绑定到 My.Settings,但在这种情况下,我们只希望它作为 datagridview 中的可编辑字符串列表。
当然,某些 My.Settings 条目可以具有不同的数据类型,这会使事情变得复杂,但通常我们只使用字符串和布尔值。
另外,我们假设用户了解他必须输入字符串“true”才能将布尔值设置为 true。不需要复选框列。
这是我们正在使用的(并且它有效),只是寻找一种更好、更精简的方式:
这是类:
Public Class MySettingsMaint
...
然后我们有一个绑定列表(这可能是yuk):
Private list As BindingList(Of BindingKeyValuePair)
这是 datagridview 绑定到的内容:
Public ReadOnly Property DataSource() As Object
Get
list = New BindingList(Of BindingKeyValuePair)
list.Add(New BindingKeyValuePair("PhoneExtension", My.Settings.PhoneExtension.ToString.ToLower))
list.Add(New BindingKeyValuePair("PhonePIN", My.Settings.PhonePIN.ToString.ToLower))
list.Add(New BindingKeyValuePair("PhoneEnabled", My.Settings.PhoneEnabled.ToString.ToLower))
Return From k In list Order By k.Key
Return list
End Get
End Property
...
Public Sub LoadGrid(ByVal grd As DataGridView) Implements
/some stuff here to create two DataGridViewColumns
/one bound to Key, the other bound to Setting
End Sub
更多yuk 这里,保存回 My.Settings:
Public Sub Save() Implements IMaintainable.Save
My.Settings.PhoneExtension = (From x As BindingKeyValuePair In list Where x.Key = "PhoneExtension" Select x.Value.ToLower).ToList(0)
My.Settings.PhonePIN = (From x As BindingKeyValuePair In list Where x.Key = "PhonePIN" Select x.Value.ToLower).ToList(0)
My.Settings.PhoneEnabled = (From x As BindingKeyValuePair In list Where x.Key = "PhoneEnabled" Select x.Value.ToLower).ToList(0) = "true"
My.Settings.Save()
End Sub
上述类所需的私有类:
Private Class BindingKeyValuePair
Sub New(ByVal k As String, ByVal v As String)
Key = k
Value = v
End Sub
Private _Key As String
Public Property Key() As String
/basic getsetter
End Property
Private _Value As String
Public Property Value() As String
/basic getsetter
End Property
End Class
How can we databind a datagridview to some of the My.Settings (user scoped) properties so the user can edit the values? This must be to a datagridview. I know we can bind to My.Settings in a form with textboxes and so on, but in this case we just want it as list of editable strings in a datagridview.
Of course some My.Settings entries can have different datatypes which complicates matters but generally we're only working with strings and booleans.
Also, let's assume the user understands that he must enter the string "true" to set a boolean to true. No checkbox column needed.
Here's what we are using (and it works), just looking for a better, leaner way:
here's the class:
Public Class MySettingsMaint
...
then we have a bindinglist (this is probably yuk):
Private list As BindingList(Of BindingKeyValuePair)
here's what the datagridview binds to:
Public ReadOnly Property DataSource() As Object
Get
list = New BindingList(Of BindingKeyValuePair)
list.Add(New BindingKeyValuePair("PhoneExtension", My.Settings.PhoneExtension.ToString.ToLower))
list.Add(New BindingKeyValuePair("PhonePIN", My.Settings.PhonePIN.ToString.ToLower))
list.Add(New BindingKeyValuePair("PhoneEnabled", My.Settings.PhoneEnabled.ToString.ToLower))
Return From k In list Order By k.Key
Return list
End Get
End Property
...
Public Sub LoadGrid(ByVal grd As DataGridView) Implements
/some stuff here to create two DataGridViewColumns
/one bound to Key, the other bound to Setting
End Sub
more yuk here, to save back to My.Settings:
Public Sub Save() Implements IMaintainable.Save
My.Settings.PhoneExtension = (From x As BindingKeyValuePair In list Where x.Key = "PhoneExtension" Select x.Value.ToLower).ToList(0)
My.Settings.PhonePIN = (From x As BindingKeyValuePair In list Where x.Key = "PhonePIN" Select x.Value.ToLower).ToList(0)
My.Settings.PhoneEnabled = (From x As BindingKeyValuePair In list Where x.Key = "PhoneEnabled" Select x.Value.ToLower).ToList(0) = "true"
My.Settings.Save()
End Sub
the private class required by the above class:
Private Class BindingKeyValuePair
Sub New(ByVal k As String, ByVal v As String)
Key = k
Value = v
End Sub
Private _Key As String
Public Property Key() As String
/basic getsetter
End Property
Private _Value As String
Public Property Value() As String
/basic getsetter
End Property
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 PropertyGrid 控件?它将有助于支持更丰富的财产类型。
You should use the PropertyGrid control? It will help with regard to supporting richer property types.