Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
215
MultiCellOperation Paste
posted

Hi There,

I have a requirement to handle the "MultiCellOperation Paste" Can someone suggest a solution?

I have already set the property

grid1.DisplayLayout.Override.AllowMultiCellOperations = AllowMultiCellOperation.Paste;

Validation

Condition

Error Message

Date Column

Only dates allowed – US formatted

Invalid Date value(s)

Dropdown Column

Only dropdown values are allowed

Item(s) not found in the dropdown list

Numeric column – INT

Only integers allowed – with size restriction

Invalid number(s)

Numeric column – DECIMAL

Only integer and decimals are allowed – with size restriction

Invalid number(s)

Alpha-Numeric column

Size restricted – max length

Input text exceeds the size

Number of rows

Only the available rows copied

NO ERROR MESSAGE

Overwrite values

Check if the cells has any existing data, and question the user to proceed further

Do you want to overwrite the existing values?

I want to do all these validations by myself and show the custom errors, 

Thanks.

Parents
  • 2680
    Verified Answer
    Offline posted

    Hello Dhanasekaran,

    Thank you for posting to Infragistics Community!

    I have been looking into your question and I can suggest handling the UltrGrid’s BeforeMultiCellOperation event. Its event data provides useful information such as the operation being performed, the affected cells and their new values. Thus, you could execute validation logic on a “Paste” operation by comparing the new values for each cell (depending on its column and data type) and canceling the event unless the conditions are met. Below, you can find a small sample, demonstrating some of the cases, including validating an integer column and a drop down one. Here is a sample snippet:

    private void ultraGrid1_BeforeMultiCellOperation(object sender, Infragistics.Win.UltraWinGrid.BeforeMultiCellOperationEventArgs e)
    {
                if (e.Operation == MultiCellOperation.Paste)
                {
                    foreach (UltraGridCell cell in e.Cells)
                    {
                        if (cell.Value.ToString() != "")
                       {
                            var result = MessageBox.Show("Some of the cells to paste within \n" +
                                 "have existing data. Do you want to overwrite the existing values?", "Warning", MessageBoxButtons.OKCancel);
    
                            if (result == DialogResult.Cancel)
                            {
                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
    
                    foreach (UltraGridCell cell in e.Cells)
                    {
                        if (cell.Column.Key.ToString().Equals("Column 0") && (Int32.Parse(e.NewValues[cell].Value.ToString()) < 0 || (Int32.Parse(e.NewValues[cell].Value.ToString()) > 10)))
                        {
                            MessageBox.Show("All integers must be greater than 0 and less than 10");
    
                            e.Cancel = true;
                            break;
                        }
    
                        if (cell.Column.Key.ToString().Equals("DropDown Col")
                            && !((cell.Column.EditorComponent as UltraComboEditor).ValueList.FindByDataValue(e.NewValues[cell].Value) != null))
                        {
                            MessageBox.Show("Pasted values for \"DropDown Col\" should be of the possible items!");
    
                            e.Cancel = true;
                            break;
                        }
                        }
                    }
                }
            }
     

    The logic here is only for demo purposes, so please, feel free to further modify it, so that your specific application requirements are met.

    Regarding validating date time cell values, the UltraGrid has built-in mechanisms to parse date time values and will output an error message in case the pasted values are not successfully parsed. So, for example if the Column format is set to ‘dd-MM-yyyy’, it will accept date values with ‘dd/MM/yyyy’ format as well while still displaying them in the specified Column format after they are pasted. If, however, it is crucial that the pasted values should exactly match the required format, additional custom validation can be added following the approach from above. For instance, the TryParseExact method could be applied to compare against the specific column format:

    if (cell.Column.Key.ToString().Equals("DateColumn"))
                        {
                            CultureInfo enUS = new CultureInfo("en-US");
                            DateTime dDate;
                            bool isOfRequiredFormat = DateTime.TryParseExact(e.NewValues[cell].Value.ToString(), cell.Column.Format, enUS, DateTimeStyles.None, out dDate);
                            if (!isOfRequiredFormat)
                            {
                                MessageBox.Show("Pasted date values should match the exact date format!");
                                e.Cancel = true;
                                break;
                            }
                        }

    I hope these suggestions help you get on the right track to implementing custom validation for your project’s grid. Please, test the sample on your side and let me know of any other questions on the matter.

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer

    2043.UGMultiCellPaste.zip

  • 2680
    Offline posted in reply to Dhanasekaran Guruswamy

    Hello Dhanasekaran,

    I am glad that you find my suggestion helpful.

    Regarding the error when pasting more cells/rows than available, yes, the UltraGrid already has a built-in validation message for this and will not allow it. The default error message is quite descriptive as it informs the user on the reason and the number of cells/rows exceeding the available space. Nevertheless, if you still require to handle this on your own, a possible approach is to handle the grid’s Error event, which is also Cancelable and provides some useful arguments about the raised error. As you can read in the referenced API document, the ErrorText message could be modified, or alternatively the default dialog box can be prevented from being displayed and your own custom one can be shown instead by canceling the event.

    Attached you will find the updated sample from before, which includes a demo solution for this case:

    private void ultraGrid1_Error(object sender, ErrorEventArgs e)
            {
                if(e.ErrorType == ErrorType.MultiCellOperation
                    && e.ErrorText.Contains("Contents being pasted have more rows than what's available starting from the anchor cell."))
                {
                    MessageBox.Show("Custom error message!");
                    e.Cancel=true;
                }
            }

    I hope this helps. Let me know if I can help with anything else regarding this matter.

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer

    6366.UGMultiCellPaste2.zip

Reply Children