Can you explain what the Infragistics Grid Copy/Paste from Excel feature does? Can I do copy and paste of highlighted or selected columns or rows within the Ultra Win grid like Excel ?
Ramsey,
If you used utility to translate the code, then it may be that the Handles keyword is missing from the event and this may need to be added. Please verify that the Handles keyword is on the event handler.
Let me know if you have any questions with this matter.
Hmm. The BeforePerformAction event never fires. I took the sample, translated it to VB and ran the debugger with a break point on the first line in the BeforePerformAction event handler where it tests to see if the Action is a paste action. The breakpoint in the BeforePerformAction event is not hit but the error event is raised.
I also recreated the BeforePerformAction event handler with no code in it and put a breakpoint on the declaration. That breakpoint is not reached either but again the Error event handler in that module is reached.
I appreciate your help...I'm lost.
I tested my sample with your logic in the Error event. In C# this is:
void ultraGrid1_Error(object sender, ErrorEventArgs e){ if (e.MultiCellOperationErrorInfo.Operation == MultiCellOperation.Paste) { e.Cancel = true; e.MultiCellOperationErrorInfo.Action = MultiCellOperationErrorInfo.ErrorAction.Continue; }}
The BeforePerformAction event fires before the Error event and then only if there is an error does the Error event fire. I verified this by removing the logic to add the new rows to the data source in the BeforePerformAction when I was testing the sample.
As such if you are getting to the Error event, then I would expect the logic in the BeforePerformAction event handler may be incorrect in your application. Do you have an example that I can test on my end?
Thanks Alan. I must be missing something because I can't seem to get past the error event to reach the BeforePerformAction event. Here is what I am doing in the error event handler:
Private Sub UltraGrid1_Error(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ErrorEventArgs) Handles UltraGrid1.Error
If e.MultiCellOperationErrorInfo.Operation = MultiCellOperation.Paste Then
e.Cancel = True
e.MultiCellOperationErrorInfo.Action =
MultiCellOperationErrorInfo.ErrorAction.Continue
End If
End Sub
I have put together a sample that shows an approach that you can take to add rows to the WinGrid when pasting data from excel into the grid. Please review this sample to see if this approach will meet your needs.
The relevant code is in the BeforePerformAction event:
void ultraGrid1_BeforePerformAction(object sender, BeforeUltraGridPerformActionEventArgs e){ // Check if this is a paste operation if (e.UltraGridAction == UltraGridAction.Paste) { // the string "\r\n" is used between the rows so split the string // from the clipboard based on this value string[] split = { "\r\n" }; string[] strings = Clipboard.GetText().Split(split, StringSplitOptions.None); // The rows to paste will be the number of strings in the array int rowsToPaste = strings.Length; // If the array contains an empty string at the end then this will not need a row to be created if (strings.Length > 0 && String.IsNullOrEmpty(strings[strings.Length - 1])) { rowsToPaste--; } // Determine where the index of the grid where the paste will begin // if there is are selected cells use that range and if there aren't any selected cells, use the active row int startIndex = 0; if (ultraGrid1.Selected.Cells.Count > 0) { ultraGrid1.Selected.Cells.Sort(); startIndex = ultraGrid1.Selected.Cells[0].Row.Index; } else { startIndex = ultraGrid1.ActiveRow.Index; } // Calculate the rows needed and then add empty rows to the DataTable the // grid is bound to int rowsNeeded = rowsToPaste - (ultraGrid1.Rows.Count - startIndex); DataTable data = (DataTable)this.ultraGrid1.DataSource; for (int i = 0; i < rowsNeeded; i++) { DataRow row = data.NewRow(); data.Rows.Add(row); } }}
void ultraGrid1_BeforePerformAction(object sender, BeforeUltraGridPerformActionEventArgs e){ // Check if this is a paste operation if (e.UltraGridAction == UltraGridAction.Paste) { // the string "\r\n" is used between the rows so split the string // from the clipboard based on this value string[] split = { "\r\n" }; string[] strings = Clipboard.GetText().Split(split, StringSplitOptions.None);
// The rows to paste will be the number of strings in the array int rowsToPaste = strings.Length;
// If the array contains an empty string at the end then this will not need a row to be created if (strings.Length > 0 && String.IsNullOrEmpty(strings[strings.Length - 1])) { rowsToPaste--; }
// Determine where the index of the grid where the paste will begin // if there is are selected cells use that range and if there aren't any selected cells, use the active row int startIndex = 0; if (ultraGrid1.Selected.Cells.Count > 0) { ultraGrid1.Selected.Cells.Sort(); startIndex = ultraGrid1.Selected.Cells[0].Row.Index; } else { startIndex = ultraGrid1.ActiveRow.Index; }
// Calculate the rows needed and then add empty rows to the DataTable the // grid is bound to int rowsNeeded = rowsToPaste - (ultraGrid1.Rows.Count - startIndex); DataTable data = (DataTable)this.ultraGrid1.DataSource; for (int i = 0; i < rowsNeeded; i++) { DataRow row = data.NewRow(); data.Rows.Add(row); } }}