[英]Edit row in GridView with DropDownList
I need edit the row of GridView
in RowDataBound
event. 我需要在
RowDataBound
事件中编辑GridView
的行。
If tried edit one row with field Area
not null value I don't have problem, but if tried edit one row with field Area
null or empty value I have the classic error : 如果尝试用
Area
不是null值编辑一行我没有问题,但是如果尝试用Area
null或空值编辑一行我有经典错误:
Object reference not set to an instance of an object
你调用的对象是空的
On the line : 在线上 :
ddlCities.Items.FindByValue(hdnval.Value).Selected = true;
I think that inserted in my code this condition resolve the problem but without success : 我认为在我的代码中插入此条件可以解决问题,但没有成功:
if (!string.IsNullOrEmpty(hdnval.Value))
{
ddlCities.Items.FindByValue(hdnval.Value).Selected = true;
}
else
{
ddlCities.Items.FindByValue(hdnval.Value).Selected = false;
}
Please help me, thank you in advance. 请帮助我,谢谢你。
My code below. 我的代码如下。
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvProducts.EditIndex == e.Row.RowIndex)
{
DropDownList ddlCities = (DropDownList)e.Row.FindControl("Area");
HiddenField hdnval = (HiddenField)e.Row.FindControl("hdnArea");
string query = " Select distinct Area from ... ; ";
OdbcCommand cmd = new OdbcCommand(query);
ddlCities.DataSource = GetData(cmd);
ddlCities.DataTextField = "Area";
ddlCities.DataValueField = "Area";
ddlCities.DataBind();
if (!string.IsNullOrEmpty(hdnval.Value))
{
ddlCities.Items.FindByValue(hdnval.Value).Selected = true;
}
else
{
ddlCities.Items.FindByValue(hdnval.Value).Selected = false;
}
}
}
Please try : 请试试 :
if(ddlCities.Items.FindByValue(hdnval.Value) != null)
{
ddlCities.Items.FindByValue(hdnval.Value).Selected = true;
}
I hope I have helped you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.