cboGender
이라는 콤보 상자가 있으므로 Male
, Female
및 Unspecified
과 같은 콤보 상자에 값 (하드 코드 값)을 추가하고 싶습니다. 어떻게해야합니까? 미리 감사드립니다.C# Combobox 하드 코딩 값
-1
A
답변
0
cboGender.Items.Add (새 항목 ("남성")));
, 당신은 항목 하나 하나를 추가 할 수 있습니다 또는 전체 모음 등을 추가 할 수 있습니다 당신이comboBox
을 채울 수있는 방법을 여러 가지 방법이있을 수
0
... 추가 항목 하나 하나는 다음과 같이 할 수있다 :
하나의 문에서 같은 위의 항목을 추가comboBox1.Items.Add("Male");
comboBox1.Items.Add("Female");
comboBox1.Items.Add("Unspecified");
는 다음과 같이 할 수있다 :
comboBox1.Items.AddRange(new object[]{ "Male","Female","Unspecified"});
당신은뿐만 아니라 당신의 comboBox
에 데이터 소스로 클래스 객체의 목록을 설정할 수 있습니다. 이 같은 클래스를 만듭니다
class personGender
{
public string gender { get; set; }
}
는 다음과 같이 comboBox
의 데이터 소스를 설정합니다
List<personGender> list = new List<personGender>()
{
new personGender{gender="Male"},
new personGender{gender="Female"},
new personGender{gender="Unspecified"},
};
comboBox1.DataSource = list;
comboBox1.DisplayMember = "gender";
또는이 같은 2 선에서이 위와 같은 작업을 수행 할 수 있습니다
comboBox1.DataSource = new List<personGender>()
{
new personGender{gender="Male"},
new personGender{gender="Female"},
new personGender{gender="Unspecified"},
};
comboBox1.DisplayMember = "gender";
당신에게 또한 데이터베이스에서 레코드를 가져온 후 datasource
을 설정할 수 있습니다.
희망이 있습니다.
0
약간의 연구를 통해 질문에 대한 답변을 얻을 수 있습니다. [이 읽으십시오] (http://stackoverflow.com/questions/14143287/add-items-to-the-combobox). –