将数据库项目绑定到下拉列表(而不是第零个位置) [英] Bind database items to dropdown list other than the zeroth position

查看:50
本文介绍了将数据库项目绑定到下拉列表(而不是第零个位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我必须将数据库中的项目绑定到ID为值,名称为Text的下拉列表中.但是我想在第0个位置上选择"项目.我正在使用以下代码:

Hi all,
I have to bind items from database to the dropdown list with ID as the value and Name as Text. But I want ''select'' item in the zeroth positon. I''m using the following code:

SqlDataAdapter da1 = new SqlDataAdapter(strng, con);
DataTable dt = new DataTable();
da1.Fill(dt);
ddLogo.DataSource = dt;
//ddLogo.DataBind();
ddLogo.DataTextField = "Ename";
ddLogo.DataValueField = "ID";
ddLogo.SelectedIndex = -1;
ddLogo.DataBind();
ddLogo.Items.Insert(0, new ListItem("--select--", "--select--"));


但是当我添加行时
ddLogo.Items.Insert(0,new ListItem(-select--","--select--")));
列表中只有第零个位置.
我该怎么办.

谢谢


But when I''m adding the line
ddLogo.Items.Insert(0, new ListItem("--select--", "--select--"));
only the zeroth position is there in the list.
What I''ve to do.

Thank you

推荐答案

您可以使用
You could use AppendDataBoundItems[^] property: set it to true, add your items (you could even do it in markup), then set DataSource and call DataBind()


插入到您的数据表中,请勿重新插入.从db获取后执行此操作

Insert into your data table dont go for fresh insert. Do this after fetch from db

SqlDataAdapter da1 = new SqlDataAdapter(strng, con);
DataTable dt = new DataTable();
da1.Fill(dt);

// generate the data you want to insert
DataRow toInsert = dt.NewRow();

// Then add the new row to the collection.
row["EName"] = "--Select--";
row["ID"] = "--Select--";

// insert in the desired place
dt.Rows.InsertAt(toInsert, 0);
ddLogo.DataSource = dt;
ddLogo.DataTextField = "Ename";
ddLogo.DataValueField = "ID";
ddLogo.DataBind();


这篇关于将数据库项目绑定到下拉列表(而不是第零个位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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