본문 바로가기
C#/Winform

C# / Winform ListBox 데이터 소스에 바인딩 안될 때 해결방법 입니다.

by HyunS_ 2019. 6. 2.
728x90

Winform의 ListBox에 바인딩 할 때

 

this.listBox.DataSource = sourceList; 와 같이 바로 제네릭 리스트와 같은 데이터를

 

바로 데이터 소스에 넣게 되면 제대로 동작하지 않습니다.

 

이럴 경우 먼저 데이터 소스에 null 값을 넣어준 다음

 

데이터를 데이터 소스에 넣어주게 되면 정상적으로 데이터가 들어갑니다.

 

아래 코드는 예제 소스 입니다.

#region SetListBoxControlData(sourceList) - 리스트 박스 컨트롤 데이터를 설정합니다.

/// <summary>
/// 리스트 박스 컨트롤 데이터를 설정합니다.
/// </summary>
/// <param name="sourceList">소스 리스트</param>
private void SetListBoxControlData(List<TimeModel> sourceList)
{
	this.recordListBox.DataSource = null;

	this.sourceList = sourceList;

	this.recordListBox.DataSource = this.sourceList;

	this.recordListBox.DisplayMember = "Time";

	this.recordListBox.Refresh();
}

#endregion

 

728x90

댓글