C# 下拉框如何绑定
C# 下拉框如何绑定 --------------------编程问答-------------------- dropprovince.DataSource = myajax.Getprovince();dropprovince.DataTextField = "province_name";
dropprovince.DataValueField = "province_id";
dropprovince.DataBind(); --------------------编程问答-------------------- 用DropDownList控件
DropDownList.DataSource = DataSet.Tables["YourTaleName"];
DropDownList.DataTextField = "name";
DropDownList.DataValueField = "id";
DropDownList.DataBind();
同ls原理一样
--------------------编程问答-------------------- 楼 上都 说了! --------------------编程问答-------------------- Combobox算下拉框不,这是WinForm中的..
for example:
SqlConnection con=new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda=new SqlDataAdapter("select * from studentDetails",con);
DataSet ds=new DataSet();
sda.Fill(ds,"student");
this.ComboBox1.DataSource=ds.Tables["student"];
this.ComboBox1.DisplayMember="sname";
//如果是WebForm,可以用楼上几位说的DropDownList.. --------------------编程问答-------------------- 怎么都被说完了,没的发挥了!
刚找到一个会的问题,命苦啊 --------------------编程问答-------------------- jf --------------------编程问答-------------------- 绑定虽然可以提高开发效率,但是伸缩性不好,最好还是自己写,在说绑定你也不知道里面发生了什么 --------------------编程问答-------------------- 同意楼上 --------------------编程问答-------------------- this.combobox.datasource=dataset1.tables["tablename"];
this.combobox.displaymember=dataset1.tables["tablename"].columns["name"].tostring();
this.combobox.tag=dataset1.tables["tablename"].columns["flag"];
数据绑定 都类似 --------------------编程问答-------------------- 哈哈 --------------------编程问答-------------------- ComboBox1.DataSource = myDataSet ;//数据集
ComboBox1.DisplayMember = "person.userId" ;//表名加字段名
ComboBox1.ValueMember = "person.username" ;
this.ComboBox1.DataSource = myDataTable;//数据表
this.ComboBox1.DisplayMember = "Name";//字段名
this.ComboBox1.ValueMember = "userId";//字段名
--------------------编程问答--------------------
在ASP.NET 2.0中,可以在数据绑定时,通过设置DropDownList的AppendDataBoundItems属性为true,在数据绑定之前添加一个新的项目,
并且这个新加的项目会保存在ViewState之中。下面就是一个实现的例子:
http://dev.csdn.net/author/net_lover/e0a560edb5d34379bb5cde9135ea6bfd.html
DropDownList1.AppendDataBoundItems = true;
--------------------编程问答-------------------- ewrwerwerwer学习 --------------------编程问答-------------------- --------------------编程问答-------------------- 学习学习 --------------------编程问答-------------------- this.cboClass.DataSource = .....; --------------------编程问答-------------------- private void MainForm_Load(object sender, EventArgs e)
{
//在界面上显示数据库信息
try
{
getInformation();//刷新窗体
string select_sql = "select name from 通讯录 order by name";
OleDbDataAdapter datadapter = new OleDbDataAdapter(select_sql, con);
DataTable datable = new DataTable();
datadapter.Fill(datable);
comboBox1.DataSource = datable;
comboBox1.DisplayMember = datable.Columns[0].ColumnName;//控制绑定值的显示
comboBox1.ValueMember = datable.Columns[0].ColumnName;//控制绑定值的显示
}
catch (Exception)
{
MessageBox.Show("连接数据库失败!");
}
finally
{
clearTextBox();//清空文本框信息
}
}
其中getInformation()和clearTextBox()是我自己定义的两个方法
补充:.NET技术 , C#