如何在绑定数据的DropDownList下拉菜单中第一行添加空白项
protectedvoidGridView_DataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType=...
protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlConnection con = null;
con = new SqlConnection("Max Pool Size = 512;Server= ; User ID= ;password=j ; Initial Catalog=xx系统");
con.Open();
string sqlstr = "select name from table1";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
DropDownList ddl = (DropDownList)e.Row.FindControl("DDLname");
DataView dv = new DataView();
dv = ds.Tables["table1"].DefaultView;
ddl.DataSourceID = null;
ddl.DataSource = dv;
ddl.DataTextField = "name";
ddl.DataValueField = "name";
ddl.SelectedIndex = 0;
ddl.DataBind();
}
}
问题:我已经把数据库中table1表的name列绑定到DDLname下拉菜单中,如何在下拉菜单中第一行添加一个空白项 请各位大侠指导 多谢多谢!!!!!!
上边这段代码应该怎么修改?????!!!!!!!!! 展开
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlConnection con = null;
con = new SqlConnection("Max Pool Size = 512;Server= ; User ID= ;password=j ; Initial Catalog=xx系统");
con.Open();
string sqlstr = "select name from table1";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
DropDownList ddl = (DropDownList)e.Row.FindControl("DDLname");
DataView dv = new DataView();
dv = ds.Tables["table1"].DefaultView;
ddl.DataSourceID = null;
ddl.DataSource = dv;
ddl.DataTextField = "name";
ddl.DataValueField = "name";
ddl.SelectedIndex = 0;
ddl.DataBind();
}
}
问题:我已经把数据库中table1表的name列绑定到DDLname下拉菜单中,如何在下拉菜单中第一行添加一个空白项 请各位大侠指导 多谢多谢!!!!!!
上边这段代码应该怎么修改?????!!!!!!!!! 展开
5个回答
展开全部
1.参考下列C# 代码:绑定数据的DropDownList下拉菜单中第一行添加空白项
2.protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlConnection con = null;
con = new SqlConnection("Max Pool Size = 512;Server= ; User ID= ;password=j ; Initial Catalog=xx系统");
con.Open();
string sqlstr = "select name from table1";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
DropDownList ddl = (DropDownList)e.Row.FindControl("DDLname");
DataTable dt = ds.Tables["table1"];
DataRow dr=dt.NewRow();
dr["name"]="--请选择--";//这里为空白项的内容
dt.Rows.InsertAt(dr, 0);
DataView dv = dt.DefaultView;
ddl.DataSourceID = null;
ddl.DataSource = dv;
ddl.DataTextField = "name";
ddl.DataValueField = "name";
ddl.SelectedIndex = 0;
ddl.DataBind();
}
}
2.protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlConnection con = null;
con = new SqlConnection("Max Pool Size = 512;Server= ; User ID= ;password=j ; Initial Catalog=xx系统");
con.Open();
string sqlstr = "select name from table1";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
DropDownList ddl = (DropDownList)e.Row.FindControl("DDLname");
DataTable dt = ds.Tables["table1"];
DataRow dr=dt.NewRow();
dr["name"]="--请选择--";//这里为空白项的内容
dt.Rows.InsertAt(dr, 0);
DataView dv = dt.DefaultView;
ddl.DataSourceID = null;
ddl.DataSource = dv;
ddl.DataTextField = "name";
ddl.DataValueField = "name";
ddl.SelectedIndex = 0;
ddl.DataBind();
}
}
展开全部
【上面不变,在下面加入这些代码】
item = new Listitem();
item.Text = "你想加入的内容";//Item.Text='';//这是空白项,或者用"--请选择--"代替
item.Value = "你想返回的值"; //item.Value="0";表示这个空项的值为0;
ddl.Items.Add(item);
//这句把选择下拉列表的第一项选择为空项开始
ddl.SelectedValue = "0";
item = new Listitem();
item.Text = "你想加入的内容";//Item.Text='';//这是空白项,或者用"--请选择--"代替
item.Value = "你想返回的值"; //item.Value="0";表示这个空项的值为0;
ddl.Items.Add(item);
//这句把选择下拉列表的第一项选择为空项开始
ddl.SelectedValue = "0";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataView dv = new DataView();
dv = ds.Tables["table1"].DefaultView;
改成
DataTable dt = ds.Tables["table1"];
DataRow dr=dt.NewRow();
dr["name"]="--请选择--";//这里为空白项的内容
dt.Rows.InsertAt(dr, 0);
DataView dv = dt.DefaultView;
dv = ds.Tables["table1"].DefaultView;
改成
DataTable dt = ds.Tables["table1"];
DataRow dr=dt.NewRow();
dr["name"]="--请选择--";//这里为空白项的内容
dt.Rows.InsertAt(dr, 0);
DataView dv = dt.DefaultView;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ddl.DataTextField = "name";
ddl.DataValueField = "name";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("", "-1"));
ddl.DataValueField = "name";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("", "-1"));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ddl.Items.Insert(0, new ListItem("", ""));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询