listbox的添加新项的方法: --1--- public class ListItem { private string id = string.Empty; private string name = string.Empty; public ListItem(string sid, string sname) { id = sid; name = sname; } public override string ToString() { return this.name; } public string ID { get { return this.id; } set { this.id = value; } } public string Name { get { return this.name; } set { this.name = value; } } }
---2 添加条目---- mysql = "select Doss_Card_Move.move_id,Doss_Card_Move.move_date 移动日期,outDept.dept_name 移出部门,inDept.dept_name 移入部门,Doss_Card.device_code 公司编号,Doss_Card.device_name 设备名称,isnull(Doss_Card.device_type,'')+isnull(Doss_Card.device_size,'') 型号规格,Doss_Card_Move.move_reason 移动原因 from Doss_Card_Move left join BASE_DEPARTMENT outDept on Doss_Card_Move.out_dept_code=outDept.dept_code left join BASE_DEPARTMENT inDept on Doss_Card_Move.in_dept_code=inDept.dept_code left join Doss_Card on Doss_Card_Move.devicecard_id=Doss_Card.devicecard_id where Doss_Card_Move.state='0'"; DataTable dtMove = database.getDataTable(mysql,null,CommandType.Text);
foreach (DataRow dr in dtMove.Rows) { ListItem item = new ListItem(dr["公司编号"].ToString(), dr["设备名称"].ToString()); listBox1.Items.Add(item); } listBox1.DisplayMember="Name"; listBox1.ValueMember="ID";