一.效果很简单几个属性与Winfrom 几乎相同
Winfrom:
cobbox.DisplayMembe=“显示值” cobbox.ValueMember=”绑定值ID”
1.死绑:Winfrom几个属性一个一个添加
public void BindCob() { cmbSex.Items.Add("---请选择----"); cmbSex.Items.Add("男"); cmbSex.Items.Add("女"); cmbSex.Text = "---请选择----"; }
2.对象构造器集合一个一个添加
public void cmbRolebind() { cmbRole.DisplayMember = "U_RealName"; cmbRole.ValueMember = "U_Role"; List<UserModel> Roles = new List<UserModel>(); Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "管理员", U_Sex = "男", U_Telephone = "1234566666", U_Role = 1, U_CanDelete = false }); Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "店长", U_Sex = "女", U_Telephone = "1234566666", U_Role = 2, U_CanDelete = false }); Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "营业员", U_Sex = "女", U_Telephone = "1234566666", U_Role = 3, U_CanDelete = false }); //foreach (var Role in Roles) //{ // cmbRole.Items.Add(Role); //} cmbRole.DataSource = Roles; cmbRole.SelectedIndex = 0; }
3.活的数据库;上面是一个数据库的方法
public void binlist() { string sql = "select * from Users"; DataTable dt = db.selectdt(sql, null, false); combox.DataSource = dt; combox.DisplayMember = "U_RealName"; combox.ValueMember = "U_ID"; } winfrom
二,1.ASP.NET类似:死绑
public void bindrdolist() { List<Mylist> m1 = new List<Mylist>() { new Mylist(){id=1,name="2"}, new Mylist(){id=3,name="3"}, new Mylist(){id=4,name="4"} }; RadioButtonList1.DataSource = m1; RadioButtonList1.DataTextField = "name"; RadioButtonList1.DataValueField = "id"; RadioButtonList1.DataBind(); }
2.活绑
public void binlist() { string sql = "select * from Users"; DataTable dt = db.selectdt(sql, null, false); DropDownList1.DataSource = dt; DropDownList1.DataTextField = "U_RealName"; DropDownList1.DataTextField = "U_ID"; DropDownList1.DataBind(); }
原文地址:https://www.cnblogs.com/yijieyufu/p/12343914.html
时间: 2024-10-07 21:06:06