gpt4 book ai didi

c# - 下拉列表值重复

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:53 26 4
gpt4 key购买 nike

我的数据库中有 4 个对象,例如 zen、maruthi 和 scorpio。将值绑定(bind)到下拉列表后,我只能看到 scorpio 重复 3 次。

而不是把它当作

zen
maruthi
scorpio

..我是天蝎座

scorpio 
scorpio..

代码

List<Cab> CabTypeList = new List<Cab>();
using (DataTable table = SqlDBConnector.ExecuteSelectCommand("GetCabType", CommandType.StoredProcedure))
{
//check if any record exist or not
if (table.Rows.Count > 0)
{
//Lets go ahead and create the list of cab

foreach (DataRow row in table.Rows)
{
cab.CabType = row["CabType"].ToString();
cab.CabId = Convert.ToInt32(row["Cab_Id"]);
CabTypeList.Add(cab);
}
}
}

ASPX 页面

if (!IsPostBack)
{
CabDbAccess cabdbaccess = new CabDbAccess();
DropDownList1.DataSource = cabdbaccess.GetCabType();
DropDownList1.DataTextField = "CabType"; // the items to be displayed in the list items
DropDownList1.DataValueField = "CabId"; // the id of the items displayed
DropDownList1.DataBind();
}

最佳答案

cab 从哪里来?确保在 foreach 循环中添加新项目。

public List<Cab> GetCabType()
{
List<Cab> CabTypeList = new List<Cab>();
// to do : get your data in the data table.
foreach (DataRow row in table.Rows)
{
var cab= new Cab();
cab.CabType = row["CabType"].ToString();
cab.CabId = Convert.ToInt32(row["Cab_Id"]);
CabTypeList.Add(cab);
}
return CabTypeList;
}

关于c# - 下拉列表值重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13226131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com