We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Copy and paste row headers question.....

We were recently asked if we could allow copying of the col header from the grid so when copying to excel they could know which col it was. I added the code that makes that work and that part works fine. My problem is this. If you select the entire col in a grid and copy and then paste to the col next to in in the grid, my paste cell text event is kicking out because the col header doesnt pass my checks. Is there any easy way other then examing the clipboard to make it so when I copy to your grid it ignores the header but will still copy it to excel? I've attached a sample. If you select col one and hit ctrl-c and then click col2 header and hit ctrl-v you will get our invalid paste option. This like I said is because the header text doesnt meet the criteria in the paste cell text event. Any ideas on this?

One last question. Is there any kind of built in DIRTY switch in the grid that tells me when any cell in the entire grid has been changed?


Thanks in advance.

CopyPasteHeaders.zip

12 Replies

AD Administrator Syncfusion Team January 8, 2007 07:11 AM UTC

Hi Philip,

If you want to remove column header during a paste in a grid, you will have to handle this your self. One way is to subscribe to the ClipboardPaste and copy the column's data without column header. Here is a code snippet to show this.

Private Sub grdCB_EPA_ClipboardPaste(ByVal sender As System.Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs) Handles grdCB_EPA.ClipboardPaste

Dim model As GridModel = sender
Dim cRangeList As GridRangeInfoList = New GridRangeInfoList()
For Each range As GridRangeInfo In CopyRangeList
If range.IsCols Then
cRangeList.Add(GridRangeInfo.Cells(model.Rows.HeaderCount + 1, range.Left, model.RowCount, range.Right))
Else
cRangeList.Add(range)
End If
Next
If Not cRangeList.Equals(GridRangeInfoList.Empty) Then
model.CutPaste.CopyTextToClipboard(cRangeList)
End If

End Sub


>>>>> Is there any kind of built in DIRTY switch in the grid that tells me when any cell in the entire grid has been changed?

Use the Model.Data.Modified property to detect the cell changes in a grid. You can reset the modifed flag by calling Data.ResetModified method.

Here is a modified sample.
ModifiedCopyPasteHeaders.zip

Let me know if this helps.

Best Regards,
Haneef


PB Philip Bishop December 10, 2007 05:11 PM UTC

Ok I am revisting this because we have been asked to look in to it again. Last time it was deemed not necessary. So basically what I want the grid to do is as follows...

I want to be able to copy a whole col with its col header text that is in row zero to excel. When I copy that same col to another col in the grid I dont want it to copy the col header text and want it to only copy what is in row1 and below. Is there an easy way to accomplish this? So like if Col 1's header text in row 0 is Test and the first cell under that is 601, then I want Test and 601 to paste in to excel. Now if I click the col header next to it on my grid and click paste, I dont want it to paste the word test in the col header and I want it to paste 601 in row 1 under the col header. I've tried about a hundred diff combinations of code to make this work and I can't make it work in all situations. Do you have any ideas on this??

Thanks

Phil



AD Administrator Syncfusion Team December 10, 2007 05:36 PM UTC

I think you should be able to use the PasteCellText event to avoid pasting text into a column header. Here is code that worked for me in your sample. (Note there is an additional e.Text.length > 0 check added because Single.Parse was throwing an exception when empty cells got copied.)


Private Sub grdCB_EPA_PasteCellText(ByVal sender As System.Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridPasteCellTextEventArgs) Handles grdCB_EPA.PasteCellText

If CType(e.Style, Syncfusion.Windows.Forms.Grid.GridStyleInfo).ReadOnly Then
e.Cancel = True
End If
If e.RowIndex = 0 Then
e.Cancel = True
Return
End If
Try
If e.Text.Length > 0 Then
Dim i As Single = Single.Parse(e.Text)
Select Case i
Case 0, 601.0F To 699.0F
Case Else
Throw New Exception("")
End Select
End If
Catch
MessageBox.Show("Paste function terminated. Data doesn't match cell type or is outside of cell value limits.", _
"QUASAR: Edit Cell Value Warning", MessageBoxButtons.OK, _
MessageBoxIcon.Warning)
e.Abort = True
End Try
End Sub




PB Philip Bishop December 10, 2007 06:55 PM UTC

Thanks for the code. I had tried that in one of my hundred attempts. It will copy the col to the col without the header in the grid and with the header in excel. If you put say 601 in 1,1 and then 602 in 2,1 and then copy those 2 cells and then click the header cell of another col and click paste, it skips the 601 and only copies the 602.

The other problem I am having is when I had to add these two things to make the copy of col headers possible.

GridDragDropFlags.RowHeader + GridDragDropFlags.ColHeader


Now I can no longer say click cell 3,5 and paste the whole copied col. So for example if you select all of col 1 using col 1's header cell and then click on cell 3,5 and click paste it will give you my error message because its trying to validate the col header text. Before adding that code it would of started pasted the copied col starting in 3,5 until it ran out of room.

Any ideas?



AD Administrator Syncfusion Team December 11, 2007 10:00 AM UTC

I think I am able to avoid the problems you described by additionally handling ClipboardPaste and there conditionally either excluding the header cell from the target range of the paste or the header row from the text on the clipboard. The modified sample is attached.





CopyPasteHeaders.zip


PB Philip Bishop December 11, 2007 02:11 PM UTC

Hey thanks for the response. That seems to all work when I am going from grid to grid and copying from the grid to excel. The only problem I see in my initial testing is that I now can't copy from any outside source like excel to the grid. I tried copying a cell with value 601 from excel to my grid and it blows up in the paste event? What's weird is it only seems to consistently do that the first time you load the form. If you copy something on the form first then it sorta works from excel? So if you start the app and try and copy a 601 from excel it will blow up. If you start the app and copy your 2 cells to other cells and then copy the 601 from excel it will work. I haven't yet figured out exactly what is causing this.

Any ideas?



PB Philip Bishop December 11, 2007 02:15 PM UTC

In regards to the last post that I've posted below, it appears to be one by one trimming out my clipboard. If you copy the col you put data in to another col it works fine. Then if you copy that col to say cell 5,1 that works fine but then if you try to copy it to cell 5,2 then it chops off the 601 and only copies 602 and if you then try and paste in to 5,3 then you get nothing pasted because it trims out the 602. I think is is all related to what I pasted below. I am still looking at this. If you have any ideas that would be great.

Previous post...
Hey thanks for the response. That seems to all work when I am going from grid to grid and copying from the grid to excel. The only problem I see in my initial testing is that I now can't copy from any outside source like excel to the grid. I tried copying a cell with value 601 from excel to my grid and it blows up in the paste event? What's weird is it only seems to consistently do that the first time you load the form. If you copy something on the form first then it sorta works from excel? So if you start the app and try and copy a 601 from excel it will blow up. If you start the app and copy your 2 cells to other cells and then copy the 601 from excel it will work. I haven't yet figured out exactly what is causing this.

Any ideas?

Thanks.



AD Administrator Syncfusion Team December 11, 2007 03:09 PM UTC

You will have to make sure you only strip the header once when you do copy, then paste, then paste, within the grid. You also do not want to strip the header if the copy was done in excel, and then you try a paste in the grid.

I think the first point can be handled by setting a flag in the Clipboard.Copy event and testing this flag in Clipboard.Paste. The second point is a little harder because the only way to decide where the copy was done is to query the Clipboard directly. But this is doable in the case of grid and excel since the grid does not put many objects on the clipboard and excel does. But if the copy source is some arbitary application, there would be no shortcut to handle this last point. If you need to support this type of copy/paste between arbitary applications, the only solumn I can think of is to handle the entire copy process yourself and place a custom object on the clipboard in addition to the text object. Then, in ClipboardPaste, you could use this custom object to process the paste.

Here is you sample back working OK between excel and the grid handling the 2nd point just by checking the number of objects on the clipboard.



CopyPasteHeaders.zip


PB Philip Bishop December 11, 2007 04:21 PM UTC

Ok so I figured that out just as I got your response. The only other thing I can't figure out is when you copy a col in the grid and then paste it to a cell..That works but if you then paste to excel you dont get the header cell because of the bln variable getting reset. I am trying to work around that now.



AD Administrator Syncfusion Team December 11, 2007 04:52 PM UTC

The only way I know to do this is to restore the clipboard after the paste in the grid that removes the header. There is a grid.Model.ClipboardPasted event (it is not exposed on the grid object) that you can use to do this.




CopyPasteHeaders.zip


PB Philip Bishop December 11, 2007 07:17 PM UTC

I had tried that after searching through the forums and it works but I did have a problem. If you copy col2 to say 5,1 that works. Then go to excel and grab like 5 valid numbers 601 to 605. Then if you pasted that in to say 4,1 that works. Now if you click col header 4 and paste, it reverts back to the first copy you did of col 2? I am beginning to think this isn't possible from outside sources because of all the possible combinations you can have?



AD Administrator Syncfusion Team December 11, 2007 07:43 PM UTC

You can handle this last problem you mentioned by clearing the clipboardheadercontents when a paste comes in from excel. Here is the change at the start of clipboardpaste.

Private Sub grdCB_EPA_ClipboardPaste(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs) Handles grdCB_EPA.ClipboardPaste
If Clipboard.GetDataObject().GetFormats().GetLength(0) > 3 Then
includesHeader = False ' copy was done in excel ...
clipBoardHeaderContents = "" 'added
End If





Loader.
Live Chat Icon For mobile
Up arrow icon