下拉列表

下拉列表:
1.如何把选项放进去。
手动放:不常用。
使用代码放:
(1).使用代码逐项添加
a.造一个实体类:重写父类的ToString()方法,返回要显示的属性。
b.使用实体造对象
c.把对象添加到下拉列表的items集合中去。

案例:把民族表中的数据读取出来加载到下拉列表中去。
a.准备实体类、链接类、数据访问类。
b.在界面后台代码中,调用数据访问类,获取返回来的民族列表数据。
c.遍历列表中的每一项,把它每个加进下拉列表的Items集合中。
(/// <summary>
/// 实体类
/// </summary>
class Nation
{
public Nation(string code,string name)
{
_Code = code;
_Name = name;
}
private string _Code;

public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Name;

public string Name
{
get { return _Name; }
set { _Name = value; }
}
public override string ToString()
{
return Name;
}
}


/// <summary>
/// 数据访问类
/// </summary>
class NationDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public NationDA()
{
_Conn = new SqlConnection(DBConnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<Nation> Select()
{
List<Nation> list = new List<Nation>();
_Cmd.CommandText = "select * from nation";
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
Nation data = new Nation(_DR["Code"].ToString(),_DR["Name"].ToString());
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
}


/// <summary>
/// 后端代码
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
//添加下拉菜单

List<Nation> list = new NationDA().Select();
foreach (Nation data in list)
{
comboBox1.Items.Add(data);
}
}
}

(2).直接使用数据绑定
需要用到的属性:
DataSource - 下拉列表的数据来源,一般是实体类的集合。
DisplayMember - 要作为显示项的属性名。
ValueMember - 要作为值项的属性名。
案例:把民族表中的数据读取出来加载到下拉列表中去。
a.准备实体类、链接类、数据访问类。
b.调用数据访问类,把返回来的数据,赋给下拉列表的DataSource
c.设置下拉列表的DisplayMember和ValueMember属性。

/// <summary>
/// 实体类
/// </summary>
class Nation
{
public Nation(string code,string name)
{
_Code = code;
_Name = name;
}
private string _Code;

public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Name;

public string Name
{
get { return _Name; }
set { _Name = value; }
}
public override string ToString()
{
return Name;
}
}


/// <summary>
/// 数据访问类
/// </summary>
class NationDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public NationDA()
{
_Conn = new SqlConnection(DBConnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<Nation> Select()
{
List<Nation> list = new List<Nation>();
_Cmd.CommandText = "select * from nation";
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
Nation data = new Nation(_DR["Code"].ToString(),_DR["Name"].ToString());
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
}


/// <summary>
/// 后端代码
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
//添加下拉菜单

//使用绑定
comboBox1.DataSource = new NationDA().Select();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Code";
}
}

案例:如何为下拉列表中加上“请选择”的项
(1).使用代码逐项添加
只需在逐项添加的代码之前,加上一个“请选择”的项即可

Nation da = new Nation("-1", "===请选择===");
comboBox1.Items.Add(da);

List<Nation> list = new NationDA().Select();
foreach (Nation data in list)
{
comboBox1.Items.Add(data);
}
(2).直接使用数据绑定
需要事选在数据源(即列表集合)中添加一个“请选择”的项。绑定即可上去。

List<Nation> list = new NationDA().Select();
list.Insert(0, new Nation("-1", "===请选择==="));
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Code";
2.如何把选中项获取出来。
相关属性:SelectedItem--选中的项
使用下拉列表的SelectedItem属性来获取,获取出来的类型是Object,需要强制转换成相应的类型。然后再获得某相应属性的值。

案例:获取选中的民族的名称和Code值出来。
private void button1_Click_1(object sender, EventArgs e)
{
//label1.Text = ((Nation)comboBox1.SelectedItem).Name;
//label1.Text += ((Nation)comboBox1.SelectedItem).Code;
label1.Text = (comboBox1.SelectedItem as Nation).Name;
label1.Text += (comboBox1.SelectedItem as Nation).Code;

}
3.如何设置某一项为选中项。
遍历下拉列表中的每一项,找到与要设置为选中项的值一样的那一样,然后把这个对象赋给SelectedItem
private void button2_Click(object sender, EventArgs e)
{
//把民族代号选中
string code = textBox1.Text;
//遍历下拉菜单的每一次项,找到对应的项,设为选中项
foreach (Nation data in comboBox1.Items)
{
if (data.Code==code)
{
comboBox1.SelectedItem = data;
}
}

案例:设置下拉列表中选中项与文本框中输入的代号一致。

三其它属性
DropDownStyle - DropDown--既可以选,又可以填写。DropDownList--只能选

 

综合案例:
1.实现人员表的添加功能:

posted @ 2015-05-16 10:35  白天\不懂/夜的黑  阅读(124)  评论(0编辑  收藏  举报