GridView edition stops working after replace

2 posts, 1 answers
  1. Adriano
    Adriano avatar
    19 posts
    Member since:
    Jul 2012

    Posted 06 Aug 2012 Link to this post

    Hi,

    I'm trying to make a correction on ALL GridView data once a user select one specific value and replace all string with another one. It works fine, but once the replace is done the grid edition stops working with keypress edit.

    Do you have any sample, or any idea of what could be happening?

    private void gvMain_CellValidating(object sender, GridViewCellValidatingEventArgs e)
           {
               string newValue = string.Empty;
               int? idSelectedCombo = new Nullable<int>();
               int? idCurrent = new Nullable<int>();
               SOFact fact = ((SOFact)(e.Row.DataContext));
     
               if (e.EditingElement is Telerik.Windows.Controls.RadComboBox)
                   newValue = ((Telerik.Windows.Controls.RadComboBox)(e.EditingElement)).Text;
     
               if (!string.IsNullOrEmpty(newValue))
               {              
                       e.IsValid = true;
                       idCurrent = fact.IdOEM;
                       idSelectedCombo = GetOEMId(ref newValue); // Gets the correct ID for the new value
     
                       //User changed from INVALID value to a VALID value
                       if (idCurrent <= -10 && idSelectedCombo > 0)
                       {
                           showChangeAllDataQuestion = false;
                       }
                        
                       fact.IdOEM = idSelectedCombo;
                       fact.DescOEM = newValue;
                       break;
               }
       }
        
        private void gvMain_CurrentCellChanged(object sender, GridViewCurrentCellChangedEventArgs e)
           {
               if (executeReplaceAfterCellValidated)
               {
                   Replace(txtFind.Text, txtReplace.Text, false);
                   executeReplaceAfterCellValidated = false;
               }
           }
            
        
           private void Replace(string stringFind, string stringReplace, bool stopAtFirst)
           {
               if (!string.IsNullOrEmpty(stringFind))
               {
                   gvMain.IsBusy = true;
                   string replacedString = string.Empty, currentColumn = "OEM";
                   string oldString = string.Empty;
                   int i = 0;
                   bool breakForLines = false;
     
                   if (stopAtFirst)
                   {
                       if (gvMain.CurrentCell == null)
                           gvMain.CurrentCellInfo = new GridViewCellInfo(facts[i], gvMain.Columns[1]);
     
                       currentColumn = gvMain.CurrentColumn.UniqueName;
                       i = gvMain.Items.IndexOf((SOFact)gvMain.CurrentCellInfo.Item);
                   }
     
                   gvMain.ItemsSource = null;
     
                   for (; i < facts.Count; i++)
                   {
                       for (int iColumn = 0; iColumn < gvMain.Columns.Count; iColumn++)
                       {
                           if (gvMain.Columns[iColumn].UniqueName.Equals(currentColumn))
                           {
                               //Verify if the column to execute search is the same as the current Column
                               switch (gvMain.Columns[iColumn].UniqueName)
                               {
                                   case "OEM":
                                       if (!string.IsNullOrEmpty(facts[i].DescOEM))
                                       {
                                           oldString = facts[i].DescOEM;
                                           replacedString = Regex.Replace(facts[i].DescOEM, stringFind, stringReplace, RegexOptions.IgnoreCase);
                                           facts[i].IdOEM = GetOEMId(ref replacedString);
                                           facts[i].DescOEM = replacedString;
                                       }
                                       currentColumn = "AIOPlatform";
                                       break;
                               }
     
                               if (!oldString.Equals(replacedString) && stopAtFirst)
                               {
                                   gvMain.CurrentCellInfo = new GridViewCellInfo(facts[i], gvMain.Columns[currentColumn]);
                                   breakForLines = true;
                                   break;
                               }
                           }
                       }
     
                       //If the string searched was found and in columns was set to leave the loop
                       if (breakForLines)
                           break;
                   }
     
                   GridViewCellInfo cellInfo = gvMain.CurrentCellInfo;
     
                   //Did this way in order to simulate Rebind, and apply conditional Styles
                   gvMain.ItemsSource = facts;
     
                   gvMain.CurrentCellInfo = cellInfo;
     
                   gvMain.IsBusy = false;
               }
           }


    Sorry for the big piece of code, but I think it's all necessary to understand the whole issue.

    Thanks,
    Adriano Gaspar.
  2. Answer
    Vera
    Admin
    Vera avatar
    265 posts

    Posted 07 Aug 2012 Link to this post

    Hi Adriano,

     
    Will it be possible to open a support ticket and to send a simple runnable project illustrating the issue? We will debug it on our side in order to find what is causing the described behavior. Please take a look at this blog post on how to isolate a problem in a sample project.


    Thank you in advance.


    All the best,
    Vera
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top