2014-07-24 3 views
1

위해 나는 다음과 같은열거는 데이터 소스로 CheckBoxList

public enum Status 
{ 
    Active, 
    Inactive, 
    Deleted 
} 

같은 enum 객체를 생성하고 난 CheckBoxList에 그 enum을 바인딩 할.

내가 시도

,

chkStatus.DataSource = Status; 
chkStatus.DataBind(); 

은 가능합니까? 그렇다면 어떻게해야합니까?

답변

3

시도 :

public enum Status 
    { 
     Active = 0, 
     Inactive = 1, 
     Deleted = 2 
    } 

그리고 CheckBoxList

checkboxID.DataSource = Enum.GetNames(typeof(Status)); 
checkboxID.DataBind(); 
+0

, 나는이 시도 할 수없는입니다 ...하지만 아직도 내가 방법을 알고 싶어요 'DataTextField'와'DataValueField'를 할당 하시겠습니까? 그리고 나는 '0, 1, 2'가 필요하지 않습니다. – Jesuraja

0
Dictionary<Status, string> dict = new Dictionary<Status, string>(); 
dct.Add(Status.Active, "Active"); 
dct.Add(Status.Inactive, "Inactive"); 
dct.Add(Status.Deleted, "Deleted"); 

BindingSource src = new BindingSource(); 
src.Datasource = dict; 

((ListBox)YourCheckList).ValueMember = "key"; 
((ListBox)YourCheckList).DisplayMember = "value; 
((ListBox)YourCheckList).Datasource = src; 
((ListBox)YourCheckList).DataBind(); 

이 코드는 당신을 도울 것입니다 희망을 바인딩.

2

이 열거 고려 :

public enum Status 
{ 
    Active=1, 
    Inactive=2, 
    Deleted=3 
} 

은 그럼 당신은이 작업을 수행 할 수 있습니다 나는 몇 가지 문제를 가지고

yourCheckBoxList.DataSource= Enum 
     .GetValues(typeof(Status)) 
     .Cast<Status>() 
     .Select (s =>new KeyValuePair<int,string>((int)s,s.ToString())) 
     .ToList(); 

yourCheckBoxList.DataValueField="Key"; 
yourCheckBoxList.DataTextField="Value"; 
yourCheckBoxList.DataBind();