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
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.
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 PachilovaAssociate Software Developer
2043.UGMultiCellPaste.zip
Can you tell me how to handle the extra items copied than the available rows? I get error before the BeforeMultiCellOperation event.
I have a grid with fixed size of ROWS, for example 4 rows, but if try to paste 6 values, how do I validate this ?
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.
6366.UGMultiCellPaste2.zip
Thanks Bozhidara. I have one more requirement. I need to allow the system to paste whatever the available values.
Ex: Only 2 rows available to paste, But user copied 4 values,
System should show error message as "Only two values can be pasted" and continue pasting 2 values.
with the error event, I can handle the custom error message but it does not paste the values. Entire paste operation was cancelled.
Any thoughts for this ?
Hi Dhanasekaran,
I understand your requirement and I can confirm that the built-in grid behavior is to cancel the operation in this situation. Modifying this behavior can be achieved by following the same approach and validating and canceling the action with the help of the grid’s Error event and some logic over the event arguments, grid’s columns and rows and the Clipboard’s contents. Actually, a similar question has already been discussed in our forum, please check out this thread. In an effort to test the suggestion there, I have adapted the previous sample to fit the requirement of accepting the pasted values within the available range and rejecting the others. As you will see, the logic is pretty custom and my suggestion is to test and debug it in order to adapt it to best fit your requirement. As this is a custom workaround solution, which is not fully tested, it may not cover all possible scenarios and will require further adjustment on your side. The purpose of the sample is to give you an idea of how you may go on about implementing your requirement, however, according to our Support Policy, implementing specific application scenarios is considered out of the scope of the service. Also, please, keep in mind that we handle a single question per support case, which is to ensure that all your issues are addressed correctly.
Thank you for your cooperation.
In conclusion, please, check out the suggested sample on your side and let me know if it helps.
8255.UGMultiCellPaste3.zip