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
615
Copy/Paste a row with ActivateOnly Cells
posted

Hi:

    I have an UltraGrid which has one (1) column that is setup for ActivateOnly.  Can anyone tell me how I can copy and paste a row successfully?

    I tried changing this cell to Activation.AllowEdit in the KeyDown event before the paste and setting it back to Activation.ActivateOnly following the paste but it doesn't work (doesn't seem to set it soon enough).

Thanks,

Kevin

  • 4555
    posted in reply to Kevin Kingma

    Hey Kevin,

    I have modified your sample and I got it to work. I have added a sub called ProcessData that will do the paste as done in the sample in the following link:

    http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=9695

    Your code would look as follows:

    Private Sub UltraGrid1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UltraGrid1.KeyDown

            If e.Modifiers = Keys.Control Then

                ''''''''''''''''''''''''
                ''  Copy/Paste Logic  ''
                ''''''''''''''''''''''''
                Select Case e.KeyCode

                    Case Keys.C

                        '''''''''''
                        '' Copy  ''
                        '''''''''''
                        copieSelectedRowCount = UltraGrid1.Selected.Rows.Count

                    Case Keys.V
                        Me.ProcessData()

    ...

    End Sub

     

    Private Sub ProcessData()
            Dim theColIndex As Integer = 0 'This is the Current Active Column Index
            Dim theColCount As Integer = 0 'This is the count of columns in the Grid
            Dim theRowIndex As Integer = 0 'This is the row index that we are on

            Dim o As DataObject = DirectCast(Clipboard.GetDataObject(), DataObject)

            If (Not o.GetDataPresent("Text")) Then Return 'Make sure we have Clipboard text

            Dim theClipboardText As String = o.GetData("Text", True).ToString()

            If (theClipboardText.Length = 0) Then Return

            Dim theGridCell As UltraGridCell = Me.ultraGrid1.ActiveCell 'Attempt to get the current active cell

            'Here we set the index of the current active cell where it is located in the column collection
            If Not theGridCell Is Nothing Then 'VB Workaround
                theColIndex = theGridCell.Column.Header.VisiblePosition
            End If

            'Here we get the count of WinGrid columns
            theColCount = Me.ultraGrid1.DisplayLayout.Bands(0).Columns.Count

            'Here we initialize the Row Index (set to 0 if there are none) as well as the Row Count
            'Dim theRowIndex As Integer = IIf(Not theGridCell Is Nothing, theGridCell.Row.Index, 0)

            If Not theGridCell Is Nothing Then 'VB Workaround
                theRowIndex = theGridCell.Row.Index
            End If

            Dim theRowCount As Integer = Me.ultraGrid1.Rows.Count

            'Clean up the Clipboard text
            theClipboardText = theClipboardText.Replace(vbLf, "")

            'Remove that last "\n" from the string
            If (theClipboardText.EndsWith(vbCr)) Then
                theClipboardText = theClipboardText.Substring(0, theClipboardText.LastIndexOf(vbCr))
            End If

            'Split the string into a "Rows" collection
            Dim theDataRows As String() = theClipboardText.Split(vbCr.ToCharArray())

            Dim theDataFields As String()

            If (theDataRows.Length = 0) Then Return

            'Split the first "Row" into a "Fields" collection just so we
            'can identify if we need to add more columns
            theDataFields = theDataRows(0).Split(vbTab.ToCharArray())

            'add more columns if needed:

            'f the current column that we are on's Index minus the total columns
            'is less than the amount of fields that are in the clipboard,
            '/then we need to add as many more columns so that when we paste
            'the content, there will be just the correct amount of columns to
            'accept the text.
            If (theDataFields.Length > Math.Abs(theColIndex - theColCount)) Then

                Dim colsToAdd As Integer = theDataFields.Length - Math.Abs(theColIndex - theColCount)

                For i As Integer = 0 To colsToAdd - 1
                    'Keep on adding columns to the data model until we have enough
                    Me.UltraDataSource1.Band.Columns.Add(Guid.NewGuid().ToString())
                Next
            End If

            'add more rows if needed:

            'if the current row that we are on's index minus the total row count
            'is less than the amount of rows we are trying to paste, then we
            'need to add as many new blank rows to accomodate the ones being pasted.
            If (theDataRows.Length > Math.Abs(theRowIndex - Me.ultraGrid1.Rows.Count)) Then

                Dim rowsToAdd As Integer = theDataRows.Length - Math.Abs(theRowIndex - Me.ultraGrid1.Rows.Count)

                For i As Integer = 0 To rowsToAdd - 1
                    'keep on adding rows through the Grid until we have enough:
                    Me.ultraGrid1.DisplayLayout.Bands(0).AddNew()
                Next

            End If

            'Very important: we need to activate the cell that we originally
            'clicked on so that the paste operation (internal to the Grid) will
            'know how and where to position the content and start off on
            'the right cell.
            Me.ultraGrid1.Rows(theRowIndex).Cells(theColIndex).Activate()

        End Sub

     

     

    Magued

  • 4555
    posted

    Hi Kevin,

    Please give me more information as to what you have set for the CellClickAction, how do you do copy and paste is it bu using ctr-c, ctr -v. I have set the activation to active only and I am able to copy and paste using ctr-c-> ctr-v with no problems.

     

    Magued