本文介绍了如何在下拉列表中绑定两个列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表有三列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"));
}
}
推荐答案
select (productname + '-' + CAST( price as nvarchar(50)) ) as 'productname-price' , productid from productinfo1
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"));
这篇关于如何在下拉列表中绑定两个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!