如何在dropdownlist textfield属性中获取两列 [英] How to get two columns in dropdownlist textfield property

查看:61
本文介绍了如何在dropdownlist textfield属性中获取两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我遇到了一个小问题.我在数据库中有一个表,其中有两列名称"和姓",并且有一个下拉列表.当我进行数据绑定时,我需要让dropdownlists datatextfield包含名称和姓氏.任何帮助将非常感激.预先感谢您

Hi all, I am stuck with a little problem. I have a table in the database with two columns "name" and "surname" and i have a dropdownlist. I need to have the dropdownlists datatextfield contain both the name and the surname when i databind. Any help would be much appreciated. Thanking you in advance

推荐答案

我们有类似的事情,我们在查询本身中将两个列值组合为一个.像这样的东西:
We had something similar and we combined the two column values into one in query itself. Something like:
SELECT
  (FirstName + "," + Surname) AS Name
FROM
  MyTable



检查此
Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                con.Open();
                   //Change with your select statement .
                using (SqlCommand cmd = new SqlCommand("select * from test1", con))
                {
                    DataTable dt = new DataTable();
                    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                    adpt.Fill(dt);
                    Dictionary<int,string> lst = new Dictionary<int,string>();
                    foreach (DataRow row in dt.Rows)
                    {
                        //Add values to Dictionary
                        string val = row[1].ToString() + " , " + row[2].ToString() + " , " + row[3].ToString();
                        lst.Add(Convert.ToInt32(row[0]), val);
                    }
                    DropDownList1.DataSource = lst;
                    DropDownList1.DataTextField = "Value";
                    DropDownList1.DataValueField = "Key";
                    DropDownList1.DataBind();
                }
            } 
        }
    }





<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>




最好的问候
M.Mitwalli




Best Regards
M.Mitwalli


您也可以通过建立合并列的列表,然后绑定list<>中的下拉列表来执行此操作.或在这里有个不错的帖子...

http://stackoverflow.com/questions/9275096/merge-来自下拉列表的数据文本字段中来自数据表的2列
You can do this also by building a List for Merged Columns and then bind dropdown from list<> or a nice post is here...

http://stackoverflow.com/questions/9275096/merge-2-columns-from-datatable-in-datatextfield-from-dropdownlist


这篇关于如何在dropdownlist textfield属性中获取两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆