Question

this code is returning a NullReferenceException since the SelectedDataKey is not selected yet. How can I check if the the DataKey is selected or not..

    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;
         }                   
        }
Was it helpful?

Solution

you will need to use a different event. the databound event is for wiring the data the view. you are looking for an event to use the data after it's bound. most likely the SelectedIndexChanging and SelectedIndexChanged events

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top