由于尚未选择SelectedDataKey,因此此代码将返回NullReferenceException。如何检查DataKey是否已选中。
protected void grdRoles_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkRole");
int rolecode = Convert.ToInt32(this.grdRoles.DataKeys[e.Row.RowIndex].Value);
BusinessLayer.Customers checkRole = new BusinessLayer.Customers();
bool check = checkRole.CheckRoles(this.grdCustomers.SelectedDataKey.Value.ToString(), rolecode); //this is triggering the nullReferenceException
if (check)
{
chk.Checked = true;
}
}发布于 2012-01-04 03:03:25
您将需要使用不同的事件。数据绑定事件用于将数据连接到视图。您正在寻找一个事件,以便在绑定数据后使用该数据。很可能是SelectedIndexChanging和SelectedIndexChanged事件
https://stackoverflow.com/questions/8717175
复制相似问题