如何将两列数据绑定到一个下拉列表 [英] How to bind two columns of data to a dropdown

查看:110
本文介绍了如何将两列数据绑定到一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想使用实体框架将2列数据绑定到一个下拉列表中
例子

Hi,
i want to bind the 2 columns data into a dropdownlist using entity frame work
example

using (testingModel.testingEntities TE = new testingModel.testingEntities())
       {
           var query = from d in TE.tbl
                       where d.tblid == "Tbl1"
                       orderby d.seq_num
                       select new
                       {
                           d.item_desc,
                           d.seq_num
                       };



我想将两列item_desc和seq_num放入下拉列表,它应该是
类似于dropdown的序列项



i want the two columns item_desc and seq_num into dropdown list and it should be
like sequence items of dropdown

推荐答案


这会给你想法
Hi ,
This will Give you idea
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,> lst = new Dictionary<int,>();
                    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


查看是否有帮助:
如何将2列值绑定到组合框中 [ ^ ]
如何在下拉菜单中绑定两个cloumns值 [
See if these help:
How to bind 2 column values into the combobox[^]
How to bind two cloumns values in dropdown[^]



这篇关于如何将两列数据绑定到一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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