바인딩소스 (BindingSource)
docs.microsoft.com/ko-kr/dotnet/api/system.windows.forms.bindingsource?view=net-5.0
BindingSource 클래스 (System.Windows.Forms)
폼의 데이터 소스를 캡슐화합니다.Encapsulates the data source for a form.
docs.microsoft.com
BindingSource
는 Form
데이터를 캡슐화 하는데 사용하고
주로 Listbox
나 DataGridView
에서 사용된다.
예제

List sampleList = new List(); BindingSource bs = new BindingSource(); int sampleData = 1; public Form1() { InitializeComponent(); bs.DataSource = sampleList; } // add private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "") { sampleList.Add(textBox1.Text.ToString()); } else { MessageBox.Show("invalid data"); } listBox1.DataSource = bs; bs.ResetBindings(false); } // sort private void button2_Click(object sender, EventArgs e) { sampleList.Sort(); bs.ResetBindings(false); } // clear private void button3_Click(object sender, EventArgs e) { sampleList.Clear(); bs.ResetBindings(false); } // sample private void button4_Click(object sender, EventArgs e) { sampleList.Add($"sample data #{sampleData}"); listBox1.DataSource = bs; bs.ResetBindings(false); sampleData += 1; }
먼저 BindingSource
를 인스턴스화 시켜주고 Form load
시에 BindingSource
내에 데이터를 담아준다.
List sampleList = new List(); BindingSource bs = new BindingSource(); public Form1() { InitializeComponent(); bs.DataSource = sampleList; }
다음에는 동작마다 데이터를 담거나 없애주고 ResetBings
을 토대로 초기화 시켜주면 된다.
listBox1.DataSource = bs; bs.ResetBindings(false);
'C# > winForm' 카테고리의 다른 글
mssql 데이터베이스 연결 (0) | 2021.03.18 |
---|
댓글을 사용할 수 없습니다.