“DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中。
table1列:id,name
table2列:id,name,sort1 //table1的name=table2的sort1
DropDownList1绑定table1
GridView1绑定table2
点修改,DropDownList1显示相应的 一级分类

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from table1";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
string sql2 = "select * from table2";
SqlDataAdapter da2 = new SqlDataAdapter(sql2, conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
GridView1.DataSource = ds2;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
int id = int.Parse(this.GridView1.DataKeys[index].Value.ToString());
if (e.CommandName == "Edit")
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from table2 where id=" + id + "";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.SelectedValue = ds.Tables[0].Rows[0]["sort1"].ToString();
//Response.Write(ds.Tables[0].Rows[0]["sort1"].ToString());
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
错误:

这是为什么啊
相关推荐
DropDownList等列表类控件的SelectedValue值的安全绑定
在ASP.NET 2.0 网站页面的开发过程中,经常需要把DropDownList等列表类控件的SelectedValue值设置为一个从数据库或其他地方读取出来的值。 最简单的办法就是直接进行指定: DropDownList1.SelectedValue = "中国"; 但有的时候如果DropDownList1中没有"中国"这一项的话,赋值就会出现异常,这是我们所不愿意看到的,那么有没有一种安全的绑定方法来避免出现这样的情况呢?当然是有的,看完我传的资料,相信一定能使你有所收获,我也是网上搜集的呢~~
DropDownList绑定选择数据报错提示异常解决方案
抛出的异常信息:异常详细信息: System.ArgumentOutOfRangeException: “ddlTotalCostDiscount ”有一个无效 SelectedValue,因为它不在项目列表中。 原先给DropDownList这样赋值: this.ddlTotalCostDiscount.SelectedValue = obj.TotalCostDiscount.ToString(); 改成这样赋值: ddlTotalCostDiscount.SelectedIndex = ddlTotalCostDiscount.Items.IndexOf(ddlTotalCostDisc
ASP dropdownlist缺少值错误
'DropDownList'的SelectedValue无效,因为它在项目列表中不存在。
三个DropDownList自动生成年月日
三个DropDownList自动生成年月日,代码复制即可使用
asp.net中DropDownList控件的二级联动
asp.net(C#)中DropDownList控件的二级联动,两种方法,第一种为普通方法,第二种为从数据库绑定DropDownList的。


