C# 从下拉列表中合并数据文本字段中数据表中的 2 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9275096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:40:14  来源:igfitidea点击:

Merge 2 columns from datatable in datatextfield from dropdownlist

c#.net

提问by Alex

var id = Session["staff_id"].ToString()

//I have datatable with 5 columns

DataTable dt = function_return_Datatable(id);

dropdownlist1.DataSource = dt;

/*in DataTextField I want to merge two columns of DataTable, dt.columns [1] is First Name and dt.columns [2] is LastName*/

//I tried this way to merge them, but no results
dropdownlist1.DataTextField = dt.Columns[1].ToString()+" "+dt.Columns[2].ToString();

dropdownlist1.DataValueField = dt.Columns[0].ToString();
dropdownlist1.DataBind();

Any ideas for how to merge these two columns?

关于如何合并这两列的任何想法?

采纳答案by vc 74

You'll need a full name column in your data table as DataTextField can refer to only one single field:

您的数据表中需要一个全名列,因为 DataTextField 只能引用一个字段:

DataTable dt = function_return_Datatable(id);
dt.Columns.Add("FullName", typeof(string), "FirstName + ' ' + LastName");

dropdownlist1.DataSource = dt;
dropdownlist1.DataTextField = "FullName";
dropdownlist1.DataValueField = "ID";
dropdownlist1.DataBind();

Should do it

应该做

(you could also add this column in your SQL query)

(您也可以在 SQL 查询中添加此列)