This is a migrated thread and some comments may be shown as answers.
how to copy cell data
6 Answers 10 Views
This is a migrated thread and some comments may be shown as answers.
fabrizio
Top achievements
Rank 1
fabrizio asked on 10 Jul 2020, 01:54 PM

Is possible to copy cell content from one row to onther new added row in radgridview

winform

Thanks a lot

6 Answers, 1 is accepted

Sort by
0
Nadya
Telerik team
answered on 10 Jul 2020, 02:20 PM

Hello, Fabrizio,

RadGridView supports built-in Copy/Paste functionality, which allows you to store text in the Clipboard and then paste it in a different location. The copying functionality in RadGridView is controlled via the ClipboardCopyMode property. More information on this topic you can find here: https://docs.telerik.com/devtools/winforms/controls/gridview/copy-paste-cut

I hope this information is useful. Should you have other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

0
fabrizio
Top achievements
Rank 1
answered on 11 Jul 2020, 09:52 AM
Hello
Nadya
Thank you very much for the answer and I apologize because my question was not complete. In fact I have an unbound RadGridView, the cells in the row are filled in by hand and not from a database. To better explain, the user takes measurements from a mechanical piece and each measurement is written with the keyboard in each cell of the row. Sometimes I need to select the compiled row and paste it directly into the new row. It can be done ? Even code is fine, maybe in a one button event.
Thanks so much
Fabrizio
I am attaching a screenshot
 
0
Nadya
Telerik team
answered on 15 Jul 2020, 01:17 PM

Hi, Fabrizio,

Thank you for providing additional information.

RadGridView is not supposed to perform copy/paste operation inside the new row. However, you can easily achieve when modifying the default paste behavior by creating a custom grid. Please refer to the following example:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        for (int i = 1; i <=5; i++)
        {
          this.radGridView1.Columns.Add("Misura "+i);
        }
        this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;

    }
}

public class CustomGrid : RadGridView
{
    protected override RadGridViewElement CreateGridViewElement()
    {
        return new CustomRadGridViewElement();
    }

    public override string ThemeClassName
    {
        get
        {
            return typeof(RadGridView).FullName;
        }
    }
}

public class CustomRadGridViewElement : RadGridViewElement
{
    protected override MasterGridViewTemplate CreateTemplate()
    {
        return new CustomMasterGridViewTemplate();
    }

    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadGridViewElement);
        }
    }
}
public class CustomMasterGridViewTemplate : MasterGridViewTemplate
{
    public override void Paste()
    {
        if (Clipboard.ContainsData(DataFormats.Text))
        {
            string data = Clipboard.GetData(DataFormats.Text).ToString();

            if (data != string.Empty && this.Owner.CurrentRow is GridViewNewRowInfo)
            {
                int columnIndex = this.Owner.CurrentColumn.Index;
                string[] rowsInfo = data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string rowInfo in rowsInfo)
                {
                    string[] cellsInfo = rowInfo.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);

                    GridViewRowInfo rowToInsert = this.Owner.Rows.NewRow();
                    for (int i = 0; i < cellsInfo.Length; i++)
                    {
                        rowToInsert.Cells[i + columnIndex].Value = cellsInfo[i];
                    }
                    this.Owner.Rows.Add(rowToInsert);
                }
            }
        }
    }
}

I hope this helps. Should you have other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

0
fabrizio
Top achievements
Rank 1
answered on 18 Jul 2020, 03:33 PM

Hi Nadya
First of all thank you very much for your patience, I regret it but I can't make it work. I have tried with your code but for sure it is I who am not able to make it work. After I added the classes with the routines in overrides I don't know how to call the Paste () routine from the RadGridView on the Form. I am attaching my code.

Thank you very much and sorry again
Fabrizio

Imports Telerik.WinControls.UI
 
Public Class Form1
 
    Public Sub New()
 
        ' La chiamata è richiesta dalla finestra di progettazione.
        InitializeComponent()
 
 
    End Sub
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
 
        For i As Integer = 1 To 10
            RadGridView1.Columns.Add(New GridViewTextBoxColumn("Misura " + i.ToString))
        Next
 
        RadGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect
 
    End Sub
 
End Class
 
Public Class CustomGrid
    Inherits RadGridView
 
    Protected Overrides Function CreateGridViewElement() As RadGridViewElement
        Return New CustomRadGridViewElement()
    End Function
 
    Public Overrides Property ThemeClassName As String
        Get
            Return GetType(RadGridView).FullName
        End Get
        Set(value As String)
            MyBase.ThemeClassName = value
        End Set
    End Property
 
End Class
 
Public Class CustomRadGridViewElement
    Inherits RadGridViewElement
 
    Protected Overrides Function CreateTemplate() As MasterGridViewTemplate
        Return New CustomMasterGridViewTemplate()
    End Function
 
    Protected Overrides ReadOnly Property ThemeEffectiveType() As Type
        Get
            Return GetType(RadGridViewElement)
        End Get
    End Property
 
End Class
 
Public Class CustomMasterGridViewTemplate
    Inherits MasterGridViewTemplate
 
    Public Overrides Sub Paste()
        If Clipboard.ContainsData(DataFormats.Text) Then
            Dim dataClip As String = Clipboard.GetData(DataFormats.Text).ToString
 
            If (dataClip <> String.Empty) And TypeOf Owner.CurrentRow Is GridViewNewRowInfo Then
                Dim columnIndex As Integer = Owner.CurrentColumn.Index
                Dim rowsInfo As String() = dataClip.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
 
                For Each rowInfo As String In rowsInfo
 
                    Dim cellsInfo As String() = rowInfo.Split(New String() {vbTab}, StringSplitOptions.RemoveEmptyEntries)
                    Dim rowToInsert As GridViewRowInfo = Owner.Rows.NewRow()
 
                    For i As Integer = 0 To i < cellsInfo.Length
                        rowToInsert.Cells(i + columnIndex).Value = cellsInfo(i)
                    Next
                    Owner.Rows.Add(rowToInsert)
                Next
 
            End If
 
        End If
 
    End Sub
 
End Class
0
Nadya
Telerik team
answered on 20 Jul 2020, 11:42 AM

Hi, Fabrizio,

According to the provided code snippet, it is not clear how you initialize the grid and whether you use the default RadGridView or the custom one. Could you please make sure that you use the custom grid in your project in order to allow the logic in the overridden Paste method to be executed.

Me.RadGridView1 = New CustomGrid()

I attached a project in VB for your reference. When using Ctrl+ C/Ctrl+V key combinations you will be able to select a row and paste it into the new row as demonstrated in the gif file from my previous post. Note, that this is a sample example and may not cover all possible cases. Feel free to modify or extend it in order to best fit your scenario.

I hope this information helps.

Regards,
Nadya
Progress Telerik

0
fabrizio
Top achievements
Rank 1
answered on 20 Jul 2020, 04:04 PM

Thank you so much ,I had gone a little confused. Now everything is fine, it works fine. Thank you very much for your patience and competence

Fabrizio

Asked by
fabrizio
Top achievements
Rank 1
Answers by
Nadya
Telerik team
fabrizio
Top achievements
Rank 1
Share this question
or