This is a migrated thread and some comments may be shown as answers.

Why Is key value wronf when editing a radgrid record

1 Answer 9 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 06 Nov 2015, 08:54 PM

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

1 Answer, 1 is accepted

Sort by
0
Perry
Top achievements
Rank 1
answered on 10 Nov 2015, 12:53 PM
The problem was resolved by changing the display setting on the LinkAnalysis Bound column from false to true
Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Answers by
Perry
Top achievements
Rank 1
Share this question
or