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 ?
Hi,
I'm pretty sure that columns are not supported, but you can copy and paste selected cells or rows to and from Excel UltraWinGrid. You can also copy and paste within the grid or from one grid to another.
There's a sample included with NetAdvantage in the WinGrid Samples Explorer.
To enable this feature, use the AllowMultiCellOperation property.
I found this sample and got it working in one of my projects. However, when i try to paste in new rows at the bottom of my grid that allows new rows it just throws an error saying there are more rows to paste then exist in the grid. Is there a property I can set that will allow paste in the excel rows as new rows in the grid? If not can you think of a way around this problem?
Thanks a lot,
Jesse
Hi Jesse,
No. Excel is a Spreadsheet, so it doesn't really have a concept of adding new rows. The rows are always just there and this is not true in the grid. The grid only has one addrow at a time and the way binding in DotNet works, it would be very tricky to add more than one row at a time. Which is not to say it cannot be done, but the grid does not currently support this.You can only paste into rows that already exist in the grid.
You should Submit a feature request to Infragistics
Mike,
Do you know of a code sample to add rows during the paste operation? My goal is to add rows, as needed, like MS Access does during multi-row paste operations. This is a key requirement in the project I am working on and I was led to believe the UltraWinGrid had this functionality.
Thanks,
Ramsey
Hi Ramsey,
I don't have any sample code for this. You might want to try searching the forums for some, if you haven't already.
The only way I can think of to make this work would be for you to catch the Paste operation before it happens and then try to add in the appropriate number of rows to the grid. So the only way to do that would be to trap the BeforeMultiCellOperation event. I'm not sure that the event args are going to give you enough information to determine the number of rows you need to add, though.
Ramsey,
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); } }}
Let me know if you have any questions with this matter.
Hello,
I have followed the steps provided and I wasn't able to reproduce the behavior with the attached sample. Are you able to reproduce the behavior with the attached sample? If so, what are the exact steps that you took and what is the value of e.ErrorText in the Error event?
Hi Allan,
Thank you for your response. I think Ramsey has explained but I will recap. Take the C# sample that comes with NetAdvantage 2010.3 and add the event handler for BeforeMultiCellOperation() by double clicking the event in the propeties window for the frmCutCopyPaste form. Add a breakpoint. Then take the code you provide above for UltraGrid1_Error and replace the code in the sample. Set a breakpoint on the first line of code. Run the sample app and and select multiple rows. click Copy button. Scroll down and highlight the last row aand click the Paste button. You will notice that you hit the breakpoint in UltraGrid1_Error() first. If you continue through your code you will notice that you never hit the breakpoint in BeforeMultiCellOperation(). So the question is how do we get past the error in order to add rows to the grid and paste the contents of the clipboard?
I am still working with Ramsey regarding the pasting and adding the rows. On my end I haven't yet seen a case where the Error event was fired first. I have seen issues with the logic for adding rows where the rows wouldn't be added and then the error event would fire after that. Do you have any example that shows the error event firing first that I could debug with?
Sorry, I was referring to the previous thread. I am trying to paste multiple row into a grid. Allen had posted some sample code that would parse the clipboard and add new rows. The problem is that the Grid error event seems to be thrown before the BeforeMultiCellOperation event ( where his code resides). He created case CAS-64104-5BFMPMP to help resolve the issue. I seem to be seeing the same thing and wondered what the resolution was.
To what issue are you referring? There no issues described here, it's just a question. And the answer is yes. AllowMultiCellOperation allows you to copy and paste cells and/or rows in the grid. You can do this within the same grid, from one grid to another, from the grid to Excel and vice versa.