ComboBox SelectedItem
C#2011. 10. 27. 11:08
DataTable로 바인딩 하여 콤보박스를 표시했을 경우
선택한 Row 전체를 가져오고자 할 때
var columnValue = ComboBox.SelectedRow["ColumnName"].ToString(); 또는 var selectedRow = ComboBox.SelectedItem as DataRowView; var columnValue = selectedRow["ColumnName"].ToString();
ValueMember나 DisplayMember로 지정하지 않은 컬럼의 값으로
콤보박스를 선택하고자 할 때
콤보박스를 선택하고자 할 때
public static DataRowView GetComboSelectedItem(ComboBox cbo
, string columnName, string valueForSelect)
{
foreach (DataRowView row in cbo.Items)
{
if (row[columnName].ToString() == valueForSelect)
{
return row;
}
}
return null;
}
DataTable dt = ...;
cboCombo.DisplayMember = "Text";
cboCombo.ValueMember = "Code";
cboCombo.DataSource = dt;
cboCombo.SelectedItem
= GetComboSelectedItem(cboCombo, "OtherColumnName", "04");
콤보박스 Dictionary 로 바인딩
var list = new Dictionary<string, int>();
list.Add("1", 100);
list.Add("2", 200)
list.Add("3", 300);
combobox.DisplayMember="Key";
combobox.ValueMember="Value";
combobox.DataSource = new BindingSource(list, null);
'C#' 카테고리의 다른 글
| 오라클 BLOB 등록 및 조회 (0) | 2016.04.14 |
|---|---|
| 다른 프로젝트의 Resources.resx 파일의 리소스에 접근하기 (0) | 2012.05.07 |
| 윈도우에 등록된 확장자를 가진 파일 열기 (0) | 2011.11.16 |
| 날짜 문자열을 DateTime형으로 변환 (0) | 2011.10.27 |
| QRCode 이미지 제작(zxing 이용) (0) | 2011.08.18 |