BindingSource
개체를 DataGridView와 함께 사용할 수 있다는 것을 알고 있습니다.DataGridView 콤보 상자가있는 BindingSource
열 중 하나에 콤보 상자를 사용할 수 있으며 여전히 BindingSource
을 활용할 수 있습니까?
BindingSource
개체를 DataGridView와 함께 사용할 수 있다는 것을 알고 있습니다.DataGridView 콤보 상자가있는 BindingSource
열 중 하나에 콤보 상자를 사용할 수 있으며 여전히 BindingSource
을 활용할 수 있습니까?
네, 그렇습니다 - ComboBox with DataGridView in C#에서 살펴 : DataGridView에와 콤보 상자를 사용하여
은 어떤 문서는 별도의 바인딩 소스와 함께 콤보를 구속력 설명 -이 경우,더 이상 그 복잡하지 않지만 소프트웨어 개발 중심의 일부 데이터를하는 동안 거의 필수입니다.
나는이 같은 DataGridView를 만들었습니다. 이제 DataGridView에서 "MonthID"및 "ItemID"대신 "Month"및 "Item"을 표시하려고합니다. 기본적으로
MonthID
및
MonthName
이 저장되는 검증 테이블과 월 이름은 원본 데이터의 ID를 기반으로 표시됩니다 .
월 데이터 소스를 설정하고 월 테이블에서 선택한 다음 반환 된 데이터에서 BindingSource
을 만듭니다. 마지막으로 다음
//Adding Month Combo
DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn();
ColumnMonth.DataPropertyName = "MonthID";
ColumnMonth.HeaderText = "Month";
ColumnMonth.Width = 120;
ColumnMonth.DataSource = bindingSourceMonth;
ColumnMonth.ValueMember = "MonthID";
ColumnMonth.DisplayMember = "MonthText";
dataGridViewComboTrial.Columns.Add(ColumnMonth);
, 그는 원래 데이터에 DataGridView
을 결합 :
//Month Data Source
string selectQueryStringMonth = "SELECT MonthID,MonthText FROM Table_Month";
SqlDataAdapter sqlDataAdapterMonth = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilderMonth = new SqlCommandBuilder(sqlDataAdapterMonth);
DataTable dataTableMonth= new DataTable();
sqlDataAdapterMonth.Fill(dataTableMonth);
BindingSource bindingSourceMonth = new BindingSource();
bindingSourceMonth.DataSource = dataTableMonth;
그런 다음 그는이 DataSource
위에서 만든 BindingSource
로 사용하여 DataGridView에에 달 ComboBoxColumn을 추가합니다.