My.SettingsをDataGridViewにバインドするのに最適な方法で、エンドユーザーは変更できますか?

StackOverflow https://stackoverflow.com/questions/6030689

  •  14-11-2019
  •  | 
  •  

質問

ユーザーが値を編集できるように、My.Settings(ユーザースコープ)プロパティの一部にDataGridViewをデータベースします。これはDataGridViewにする必要があります。私たちはTextBoxesなどのフォームでmy.settingsにバインドできることを知っていますが、この場合はDataGridViewで編集可能な文字列のリストとしてそれを望みます。 もちろん、いくつかのmy.Settingsエントリには、問題を複雑にするのが異なるが、一般的に文字列やブール値を扱うだけのデータ型を持つことができます。

また、Booleanを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
.

役に立ちましたか?

解決

You should use the PropertyGrid control? It will help with regard to supporting richer property types.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top