在RowDataBound中获取SelectedDataKey

时间:2012-01-03 18:52:27

标签: c# nullreferenceexception rowdatabound

此代码返回NullReferenceException,因为尚未选择SelectedDataKey。如何检查是否选择了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;
         }                   
        }

1 个答案:

答案 0 :(得分:0)

您需要使用其他活动。数据绑定事件用于连接视图的数据。您正在寻找一个事件来绑定它后使用数据。最有可能是SelectedIndexChangingSelectedIndexChanged事件