2013-07-29 2 views
0

배경 : Sorted 속성에서만 다른 두 개의 콤보 상자가 있습니다. comboBox1의 Sorted 속성이 으로 설정되어 있고 comboBox2의 Sorted 속성이 false으로 설정되어 있습니다. 이 두 콤보 상자의 데이터 소스 속성을 다시 할당하거나 재설정하려고하면 comboBox1은 데이터를 표시하지 않고 comboBox2는 데이터를 표시하지 않습니다. Sorted 속성이 comboBox1이 '데이터를 올바르게 표시하지 못하게하는 이유는 무엇입니까?정렬 된 콤보 상자 및 데이터 소스 재 할당

모든 코드는 다음과 같다 :

public partial class Form1 : Form 
{ 
    private string[] a8BitGames = { "Metroid", "Zelda", "Phantasy Star", "SB:S&SEP" }; 
    private string[] a16BitGames = { "StarFox", "Link", "Final Fantasy", "Altered Beast" }; 
    private List<string> lSomeList = null; 
    private List<string> lSomeOtherList = null; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.lSomeList = new List<string>(a8BitGames); 
     this.lSomeOtherList = new List<string>(a16BitGames); 
     this.comboBox1.DataSource = lSomeList; 
     this.comboBox2.DataSource = lSomeOtherList; 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     this.IndexChanged(1); 
    } 

    private void IndexChanged(int comboBox) 
    { 
     this.comboBox1.DataSource = null; 
     this.comboBox1.DataSource = a16BitGames; 

     this.comboBox2.DataSource = null; 
     this.comboBox2.DataSource = a8BitGames; 
    } 

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     this.IndexChanged(2); 
    } 
} 

partial class Form1 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.comboBox1 = new System.Windows.Forms.ComboBox(); 
     this.comboBox2 = new System.Windows.Forms.ComboBox(); 
     this.SuspendLayout(); 
     // 
     // comboBox1 
     // 
     this.comboBox1.FormattingEnabled = true; 
     this.comboBox1.Location = new System.Drawing.Point(13, 13); 
     this.comboBox1.Name = "comboBox1"; 
     this.comboBox1.Size = new System.Drawing.Size(121, 21); 
     this.comboBox1.Sorted = true; 
     this.comboBox1.TabIndex = 0; 
     this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
     // 
     // comboBox2 
     // 
     this.comboBox2.FormattingEnabled = true; 
     this.comboBox2.Location = new System.Drawing.Point(13, 41); 
     this.comboBox2.Name = "comboBox2"; 
     this.comboBox2.Size = new System.Drawing.Size(121, 21); 
     this.comboBox2.TabIndex = 1; 
     this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 262); 
     this.Controls.Add(this.comboBox2); 
     this.Controls.Add(this.comboBox1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.ComboBox comboBox1; 
    private System.Windows.Forms.ComboBox comboBox2; 
} 
+0

색인에서 데이터 소스를 왜 다시 변경 하시겠습니까? –

+0

@Sriram, 이것은 예제 코드입니다. 실제 프로그램에서는 여러 콤보 상자에 공통된 값 집합을 관리하고 두 콤보 상자에서 동일한 값을 선택할 수 없습니다. 색인이 변경 될 때마다 목록에서 새 색인 항목을 제거하고 이전 색인 항목을 추가합니다. 이렇게하면 새 인덱싱 된 항목이 다른 콤보 상자에서 선택 항목으로 제거되고 이전 인덱싱 된 항목이 선택 항목으로 추가됩니다. 그래도 질문과 관련이 있는지 확신하지 못했습니다. – HighExodus

답변

1

당신은 실수로 예외를 숨기고 있습니까? MSDN에 따르면 "데이터 소스에 연결된 ComboBox를 정렬하려고 할 때"ArgumentException이 발생합니다.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.sorted.aspx

+0

동의하지만, 예외를 숨기고 있지 않습니다. 사실 true로 속성을 기본값으로 설정하면 예외가 발생하는 것을 막을 수 있을지 궁금합니다. – HighExodus

+1

comboBox1.Sorted = false를 설정하려고 할 때 오류가 발생하기 때문에 예외가 표시되지 않습니다. ComboBox that 데이터 소스 집합을 정렬 할 수 없습니다. 기본 데이터 모델을 사용하여 데이터를 정렬합니다. –

+0

HighExodus는 기본값이 아니지만 정렬을 설정하면 항목이 데이터 바인딩 된 컨트롤 (아직)이 아닙니다. this.comboBox1.Sorted = true;를 설정해보십시오. IndexChanged (...) 메서드가 끝날 때 예외가 표시됩니다. –

1

봅니다 문제는 당신은 정렬 된 콤보 상자의 데이터 소스를 변경할 수 없다는 것입니다 데이터 소스

public List<string> A16Games 
{ 
    get { return this.a16BitGames.OrderBy(x => x).ToList(); } 
} 

public List<string> A8Games 
{ 
    get { return this.a8BitGames.OrderBy(x => x).ToList(); } 
} 

this.comboBox1.DataSource = this.A16Games; 
this.comboBox2.DataSource = this.A8Games; 
+0

@Martjin van Put, thanks Martin. 이 방법은 콤보 상자에 표시되는 목록을 정렬하는 데 사용할 수있는 유효한 방법입니다. 그러나 .NET이 comboBox1에서 올바르게 (정렬 된) 초기 데이터 소스 할당을 허용 한 이유에 더 관심이 있었지만 다른 모든 시도는 실패합니다. – HighExodus

+0

@HighExodus, 오신 것을 환영합니다! 그리고 실제로 그것은 정렬과 함께 데이터 소스 위의 답변에서 설정되지 않을 수있다 Paul Zaczkowski –

1

설정에 대한 목록을 정렬하여 정렬합니다. 당신은 InvalidOperationException이를 받으셨을 것입니다

 protected override void OnDataSourceChanged(EventArgs e) 
    { 
     if ((this.Sorted && (this.DataSource != null)) && base.Created) 
     { 
      this.DataSource = null; 
      throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ComboBoxDataSourceWithSort")); 
     } 
     ... 
     ... 
    } 

같은 경우 : 다음은 콤보 상자 컨트롤의 코드의 발췌 한 것입니다. 수신하지 않은 이유는 무엇입니까?

 public object DataSource 
    { 
     get 
     { 
      return base.DataSource; 
     } 
     set 
     { 
      base.DataSource = value; 
     } 
    } 

그리고 기본 클래스의 데이터 소스에서 :

ComboBox 컨트롤에서 데이터 소스 속성의 구현은 (ListControl의) 구현의 기본 클래스로 리디렉션 :

 public object DataSource 
    { 
     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 
     get 
     { 
      return this.dataSource; 
     } 
     set 
     { 
      if (((value != null) && !(value is IList)) && !(value is IListSource)) 
      { 
       throw new ArgumentException(System.Windows.Forms.SR.GetString("BadDataSourceForComplexBinding")); 
      } 
      if (this.dataSource != value) 
      { 
       try 
       { 
        this.SetDataConnection(value, this.displayMember, false); 
       } 
       catch 
       { 
        this.DisplayMember = ""; 
       } 
       if (value == null) 
       { 
        this.DisplayMember = ""; 
       } 
      } 
     } 
    } 

하시기 바랍니다 여기 는 답변입니다 침묵에주의하십시오 잡아 블록. SetDataConnection은 OnDataSourceChanged를 호출하므로 내 분석이 올바른 경우에만 이것이 이유입니다. 내가 틀렸다면 제발 고쳐주세요.