给DropDownList的DataTextField属性绑定两个字段
2011-06-24 09:53
429 查看
给DropDownList的DataTextField属性绑定两个字段
2010年01月03日 星期日 20:29
方法1:在SQL查询语句中拼接字段
C#代码
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 ProductId,ProductName+','+convert(nvarchar,UnitPrice) Text FROM Products", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
DropDownList1.DataSource = table1.DefaultView;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "ProductId";
DropDownList1.DataBind();
}
方法2:在DataTable中新增一字段
C#代码
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 ProductId,ProductName,UnitPrice FROM Products", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
table1.Columns.Add("Text", System.Type.GetType("System.String"), "ProductName+'-'+UnitPrice");
DropDownList1.DataSource = table1.DefaultView;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "ProductId";
DropDownList1.DataBind();
}
2010年01月03日 星期日 20:29
方法1:在SQL查询语句中拼接字段
C#代码
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 ProductId,ProductName+','+convert(nvarchar,UnitPrice) Text FROM Products", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
DropDownList1.DataSource = table1.DefaultView;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "ProductId";
DropDownList1.DataBind();
}
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;")) { SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 ProductId,ProductName+','+convert(nvarchar,UnitPrice) Text FROM Products", conn); DataTable table1 = new DataTable(); adapter1.Fill(table1); DropDownList1.DataSource = table1.DefaultView; DropDownList1.DataTextField = "Text"; DropDownList1.DataValueField = "ProductId"; DropDownList1.DataBind(); }
方法2:在DataTable中新增一字段
C#代码
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 ProductId,ProductName,UnitPrice FROM Products", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
table1.Columns.Add("Text", System.Type.GetType("System.String"), "ProductName+'-'+UnitPrice");
DropDownList1.DataSource = table1.DefaultView;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "ProductId";
DropDownList1.DataBind();
}
相关文章推荐
- 给DropDownList的DataTextField属性绑定两个字段
- 给DropDownList的DataTextField属性绑定两个字段
- [习题]DropDownList的子选项(DataTextField),出现两个字段
- [技巧]DataGird的hyper column的url field 绑定两个字段
- DataTextField绑定两个数据字段
- DropDownList数据绑定及SelectedValue DataTextField 用法
- DataTextField 绑定多个字段
- 将SQLServer数据库中的Image字段绑定到PictureBox控件及DataBindings属性
- 在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true
- 使用DataSource绑定一维数组时,DataTextField只需绑定空字符串
- 如何在dropdownlist动态添加item之后,如何设置其value值! /用JS控制服务端dropDownlist和textField的Readonly属性
- 通过晚期绑定读写属性Property和字段Field
- DropDownlist的DataTextField显示多列数据
- 绑定 DataBinder.Eval(Container.DataItem,"字段")与 Eval("字段") 区别?
- (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
- 分享初学者的第一个WPF应用程序——Timer线程时时刷新当前系统时间赋值于相关属性,并将其值绑定于TextBlock的Text属性
- 尽可能使用属性(Property),少使用字段(Field)实现对数据的访问 (轉 )
- IOS TextField 属性设置大全
- ExtJs中TextField与TextArea的只读属性设置
- DataTextField和DataValueFiedl的区别