gpt4 book ai didi

c# - 来自数据库的下拉列表,如何合并 DataTextField 的列?

转载 作者:行者123 更新时间:2023-11-30 20:53:57 25 4
gpt4 key购买 nike

我有一个从数据库中填充的下拉列表。当我只想在下拉列表的 DataTextField 中显示名字时,我可以使用它。但是如果我想显示 2 个字段,名字和姓氏,则会抛出错误。为什么这不起作用或我怎样才能让它起作用?

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_customers", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(ds, "Customers");
CustomerList.DataSource = ds.Tables[0];
CustomerList.DataTextField = "FirstName" + " " + "LastName";
CustomerList.DataValueField = "CID";
CustomerList.DataBind();

最佳答案

您必须从数据库中选择“虚拟”列:

string sql = @"SELECT FirstName + ' ' + LastName AS FullName, 
CID, FirstName, LastName
FROM tbl_customers
ORDER BY FullName ASC;"
SqlCommand cmd = new SqlCommand(sql, con);
// ...
CustomerList.DataSource = ds.Tables[0];
CustomerList.DataTextField = "FullName";
CustomerList.DataValueField = "CID";
CustomerList.DataBind();

关于c# - 来自数据库的下拉列表,如何合并 DataTextField 的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19522112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com