如何在下拉列表中绑定两个列值 [英] How to bind two column values in dropdownlist

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

问题描述

我的表有三列productid,productname,price



在下拉列表中我想显示所有productname-price from database。

<下拉菜单中的
我需要如下所示



iphone-15000

oneplus-2000



全部在gridview专栏中。



我尝试了什么:



my table has three columns productid,productname,price

In dropdownlist i want display all productname-price from database.

in dropdown i need like this below

iphone-15000
oneplus-2000

all in gridview column.

What I have tried:

protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         SqlConnection con1 = new SqlConnection(@"user id=sa;password=ssa;database=Mohan;data source=PCTH101\PCTH101");
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
           con1.Open();
             var ddl = (DropDownList)e.Row.FindControl("DropDownList3");
             int productid = Convert.ToInt32(e.Row.Cells[0].Text);
             SqlCommand cmd = new SqlCommand("SELECT * from productinfo1", con1);
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             DataSet ds = new DataSet();
             da.Fill(ds);
             con1.Close();
             ddl.DataSource = ds;
             ddl.DataTextField = "productname-price";
          //   ddl.DataTextField = "price";
             ddl.DataValueField = "productid";
             ddl.DataBind();
             ddl.Items.Insert(0, new ListItem("--Select--", "0"));
         }

     }

推荐答案

在sql中可能最容易做到查询 - 将两列连接到显示值: sql - 连接两个数据库列到一个结果集列 - Stack Overflow [ ^ ]
May be easiest to do in the sql query - join two columns into a display value: sql - concatenate two database columns into one resultset column - Stack Overflow[^]


更改查询

change the query
select  (productname + '-' + CAST( price as nvarchar(50)) ) as 'productname-price' , productid from productinfo1


此代码工作正常....

This code is working fine....
SqlCommand cmd = new SqlCommand("select productid, productname + ' - ' + price as new from productinfo1", con1);
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             DataSet ds = new DataSet();
             da.Fill(ds);
             con1.Close();
             ddl.DataSource = ds;
             ddl.DataTextField = "new";
             //   ddl.DataTextField = "price";
             ddl.DataValueField = "productid";
             ddl.DataBind();
             ddl.Items.Insert(0, new ListItem("--Select--", "0"));


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

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