2009-01-31 4 views
7

나는 키 값 쌍에 대한 참조 잡아 내 DB에있는 테이블의 숫자가 : 전화 번호의gridview에 바인드 드롭 다운리스트는 목록으로 <keyvaluePair <int, string>>

유형 :

  • 1 - 홈
  • 2 - 일
  • 3 - 모바일
  • 4 - 팩스

그래서 형식에 대한 테이블이 있고 다른 테이블에서 사용되는 경우 외래 키로 int 값을 참조합니다. 내가 그 (것)들을 끌고있을 때 나는 그들을 사용하고있는 종류에있는 keyvaluepair<int, string> 품목으로 그 (것)들을 저장하고있다.

필자는 목록을 가져와야 할 때 동일한 데이터를 얻기 위해 두 가지 유형의 데이터 형식을 사용하는 대신 목록 <을 작성한다고 생각했습니다.

edittemplate 비트를 사용할 때 gridview 내에서 드롭 다운 목록을 채워야 할 때 제 문제가 발생했습니다. 데이터 소스를 사용하여 이것을 꺼내면 int를 값으로, Home을 표시 할 텍스트로 넣는 대신 텍스트에 [1 Home]을 씁니다.

나는 여러 부분으로 된 질문을 가지고 있다고 생각합니다.

하나 :

나는 바보인가? 데이터를 가져 와서 저장하는 것은 정말 나쁜 방법입니까 (keyvalue 쌍 부분)? 난 그냥 모든 데이터 테이블에 저장해야합니까? 나는 그것을 모두 datatable에 넣는 것을 좋아하지 않았습니다. 나는 내 BLL을 가지고 나의 DLL을 가지고 있고 모든 것을 객체로 캡슐화하려고했거나 List<>이 모든 것의 테이블이 아니라 캡슐화하려고 시도했다. 대부분 이것은 잘 작동했습니다.

2 :

난 그냥 목록의 첫 번째 항목을 가지고있는 것이 아니라, 현재 선택된 값을 설정하는 방법, 어떤 객체가 아닌 DropDownList로 내 ObjectDataSource를에 결합 할 수있는 데이터 테이블을 사용하는 경우 선택된? 내가 바보되고 그냥 DataValueField 및 DataKeyField을 설정하는 데 필요한되었다 아래

편집

으로는 지적했다. 내가 수동으로 입력 할 때

SelectedValue='<%# DataBinder.Eval(Container, "DataItem.PhoneType.Key") %>' 

내 인텔리에 표시되지 않았기 때문에 내가 바로했다 그 중 하나가 표시되지 않은 이유를하지만 :

난 그냥해야 할 일을했을 바인딩 DropDownList로를 얻으려면 안으로, 그것은 일했다.

답변

29

사용하여 Dictionary < INT, 문자열 > 및 및 DataTextField에 드롭 다운 DataValueField을 설정합니다.

// A sample dictionary: 
    var dictionary = new Dictionary<int, string>(); 
    dictionary.Add(1, "Home"); 
    dictionary.Add(2, "Work"); 
    dictionary.Add(3, "Mobile"); 
    dictionary.Add(4, "Fax"); 

    // Binding the dictionary to the DropDownList: 
    dropDown.DataTextField = "Value"; 
    dropDown.DataValueField = "Key"; 
    dropDown.DataSource = dictionary; //Dictionary<int, string> 
    dropDown.DataBind(); 
+0

Dayam - 난 내가 * 쉽게 * 30 점 정도 줄 알았는데하지만 당신은 그것에 나를 이길. 좋은 일과 좋은 대답! –

+1

감사합니다 .. 그걸 훌륭하게 묶었습니다. 그것도 List <>와 함께 작동했습니다. 하지만 어쨌든 사전이 어떤 의미인지 짐작합니다 ... – Jon

+0

DataValueField 및 DataTextField를 설정하는 것을 잊었습니다. 내 값을 발생시키고 드롭 다운 목록의 텍스트를 표시하는 것은 동일합니다. 감사! –

0

하고 내 사용자 지정 방법

// Define enum 
public enum ServiceType : int 
{ 
    MinimumService = 1, 
    NormalService = 2, 
    VipService = 99 
} 

// custom method to get my custom text name for each enum type 
public string GetServiceTypeName(ServiceType serviceType) 
{ 
    string retValue = ""; 
    switch (serviceType) 
    { 
     case ServiceType.Print: 
      retValue = "We have some services for you"; 
      break; 
     case ServiceType.BookBinding: 
      retValue = "We ar glad to meet you"; 
      break; 
     default: 
      retValue = "We alywas are ready to make you happy"; 
      break; 
    } 
    return retValue; 
} 

// making dictionary (key and value) for dropdown datasource 
public static Dictionary<string, int> GetAllServiceTypeName() 
{ 
    Dictionary<string, int> dataSource = new Dictionary<string, int>(); 

    foreach (int item in Enum.GetValues(typeof(ServiceType))) 
     dataSource.Add(GetServiceTypeName((ServiceType)item), item); 

    return dataSource; 
} 


    // bind the dropdown to dictionary 
    ddlServiceType.DataSource = GetAllServiceTypeName(); 
    ddlServiceType.DataBind(); 

    // aspx markup code sample 
    <asp:DropDownList ID="ddlServiceType" runat="server" 
     DataTextField="Key" DataValueField="Value"> 
    </asp:DropDownList> 
관련 문제