My.settings를 DataGridView에 바인딩하는 가장 좋은 방법이므로 최종 사용자가 수정할 수 있습니까?

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

  •  14-11-2019
  •  | 
  •  

문제

어떻게 My.Settings (사용자 범위) 속성 중 일부에 DataGridView를 DataGrIdView에서 데이터를 편집 할 수 있습니까? 이것은 DataGridView에 있어야합니다. 나는 우리가 TextBoxes가있는 양식에있는 폼에서 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
.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top