Radgrid data is empty after updating record

109 Views Asked by At

I have a radgrid binded with entity datasource.Everything works well except that when I update record from code behind on updateCommand the data is lost from grid and in database the record is updated.please help with this.below is the code for updting the record.

Thanks in advace.

 protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                 var editItem = (GridEditableItem)e.Item;
                 var values = _extractEpisodePrograms(editItem);
                 var gettingEpisodeProgramDiagnosis_ID = (int)editItem.GetDataKeyValue("EpisodeProgramDiagnosis_ID");
                 using (HSS_Swirl_WebEntities context = new HSS_Swirl_WebEntities())
                 {
                     var EpisodeProgramDiagnosis = context.ClientEpisodeProgramDiagnosis.First(t => t.EpisodeProgramDiagnosis_ID == gettingEpisodeProgramDiagnosis_ID);
                     EpisodeProgramDiagnosis.EpisodeProgram_ID = 39;
                     EpisodeProgramDiagnosis.ConditionOnset = (bool)values["ConditionOnset"];
                     EpisodeProgramDiagnosis.OtherDiagnosis = false;
                     EpisodeProgramDiagnosis.PrimaryDiagnosis = (bool)values["PrimaryDiagnosis"];
                     EpisodeProgramDiagnosis.ICD10Diagnosis_ID = 11;
                     var CheckingDuplicateKeys = (from p in context.ClientEpisodeProgramDiagnosis where p.EpisodeProgram_ID == EpisodeProgramDiagnosis.EpisodeProgram_ID && p.ICD10Diagnosis_ID == EpisodeProgramDiagnosis.ICD10Diagnosis_ID select p).FirstOrDefault();
                     if (CheckingDuplicateKeys == null)
                     {
                         context.SaveChanges();
                     }
                     else {
                         ShowErrorMessage("You cannot enter duplicate values for ICD10Diagnosis_ID and EpisodeProgram_ID");
                     }
                 }
            }
            catch (Exception ex)
            {

                var insertException = new CustomValidator();
                insertException.IsValid = false;
                insertException.ErrorMessage = (ex.InnerException != null) ? ex.InnerException.ToString() : ex.Message;
                Page.Validators.Add(insertException);
            }
            RadGrid1.Rebind();
        } 
1

There are 1 best solutions below

0
On

Try setting you datasource right in the RadGrid's NeedDataSource event like so:

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = myDataSource;
}

Remember to wire RadGrid1_NeedDataSource to the OnNeedDataSource event in RadGrid1.

You could also try setting the datasource again right before you rebind like so:

RadGrid1.DataSource = myDataSource;
RadGrid1.Rebind();