Why Is key value wronf when editing a radgrid record

2 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 06 Nov 2015 Link to this post

    I have the following setup in my RadGrid lines in bold contain my database table primary key named LinkAnalysisId

    <Columns>
    <telerik:GridEditCommandColumn ButtonType="ImageButton">
    </telerik:GridEditCommandColumn>
    <telerik:GridBoundColumn DataField="LinkAnalysisId" HeaderText="Link ID" ReadOnly="true"
    ForceExtractValue="Always" ConvertEmptyStringToNull="true" Display="False" />
    <telerik:GridHyperLinkColumn FilterControlAltText ="Filter LinkID2 column"
    Uniquename="LinklId2" DataNavigateUrlFormatString ="/SubjectList2.aspx?LinkID={0}"
    DataTextField="LinkAnalysisName" HeaderText="Link Analysis Name"
    DataNavigateUrlFields="LinkAnalysisId" />

    <telerik:GridBoundColumn DataField="LinkAnalysisName" HeaderText="LinkAnalysisName" SortExpression="LinkAnalysisName"
    UniqueName="LinkAnalysisName" Display="False">

    Other bound columns are not shown  when I update a records and try to save I run this C# code

    protected void RadGrid1_UpdateCommand(object sender, Web.UI.GridCommandEventArgs e)
            {        
                   var editableItem = ((GridEditableItem)e.Item);
     
               var linkAnalysisId = (int)RadGrid1.MasterTableView.Items[0].GetDataKeyValue("LinkAnalysisId");
               
                  //retrive entity from the Db
     
               var record = DbContext.LinkAnalysis.Where(n => n.LinkAnalysisId == linkAnalysisId).FirstOrDefault();
     
                  if (record != null)
                  {
                      //update entity's state
                      editableItem.UpdateValues(record);
                      try
                      {
                          //save chanages to Db
                          DbContext.SaveChanges();
                      }
                      catch (System.Exception)
                      {
                          ShowErrorMessage();
                      }
     
                 }
           }
         The save does not pass the try catch block  I have done some trouble shooting and I have found the code      

    var linkAnalysisId = (int)RadGrid1.MasterTableView.Items[0].GetDataKeyValue("LinkAnalysisId");

    which sets the linkAnalysis variable is not getting the correct ID number so it appears I have something wrong. In debug mode when I inspect the values in the var editableItem I am seeing the correct linkAnalysis ID number it is just the code above is not getting the right record what have I done wrong here?

     

    Thanks

  2. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 10 Nov 2015 in reply to Perry Link to this post

    The problem was resolved by changing the display setting on the LinkAnalysis Bound column from false to true
Back to Top