Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF Datagrid将网格的最后一行复制到网格中的新行(Devexpress)_C#_Wpf_Datagrid_Devexpress_Devexpress Wpf - Fatal编程技术网

C# WPF Datagrid将网格的最后一行复制到网格中的新行(Devexpress)

C# WPF Datagrid将网格的最后一行复制到网格中的新行(Devexpress),c#,wpf,datagrid,devexpress,devexpress-wpf,C#,Wpf,Datagrid,Devexpress,Devexpress Wpf,有一件事我想做,但我把它当成了一个傀儡。这是步骤 当用户点击Datagrid上的ctrl+D按钮时 最后一行数据网格值将复制到Clippoard或其他地方,例如CopyToClipboard func。 从剪贴板或其他东西,它可能是一个函数,例如pastToclipboard,或者我们可以使用InitNewRowEventArgs,它让我们访问RowHandle函数 这是我到目前为止完成的代码 private void dtg_tabletrial_KeyDown(object sender,

有一件事我想做,但我把它当成了一个傀儡。这是步骤

当用户点击Datagrid上的ctrl+D按钮时 最后一行数据网格值将复制到Clippoard或其他地方,例如CopyToClipboard func。 从剪贴板或其他东西,它可能是一个函数,例如pastToclipboard,或者我们可以使用InitNewRowEventArgs,它让我们访问RowHandle函数 这是我到目前为止完成的代码

private void dtg_tabletrial_KeyDown(object sender, KeyEventArgs e)
 {
  if (e.Key == Key.D && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {  
          MessageBox.Show("You hit ctrl + D");
        }
 }

 private void dtg_view_InitNewRow(object sender, DevExpress.Xpf.Grid.InitNewRowEventArgs e)
    {
        dtg_tabletrial.SetCellValue(e.RowHandle, "UserName", "emre");
        dtg_tabletrial.SetCellValue(e.RowHandle, "Surname", "newcompany");
        dtg_tabletrial.SetCellValue(e.RowHandle, "Address", "new addres");
        dtg_tabletrial.SetCellValue(e.RowHandle, "Phone", "new phone");   
    }

我假设“最后一行”是指由当前应用的排序和筛选确定的最后一个可见行,即滚动到底部时最后出现的行,此时可能不在视图中

有两种方法可以做到这一点,但我认为这是最简单的方法,并且它不需要将后备行对象标记为[Serializable]:


再次感谢Mike Strobel,但我还提供了另一个解决方案。我在这里写信给需要它的人

和平


“最后一行”是指:源集合中的最后一个对象;根据过滤条件,最后一行“可见”,在视口中不一定可见;或者是最近选定的行?我是指datagrid的最后一个可见行。我可以将目标和源定义为同一个datagrid吗?是的,这应该非常好。
private void CopyLastRowToAnotherGrid()
{
    const int targetHandle = DataControlBase.NewItemRowHandle;

    GridControl source = /* your first grid */;
    GridControl target = /* your second grid */;

    if (source.VisibleRowCount < 1|| !target.IsValidRowHandle(targetHandle))
        return;

    var sourceHandle = source.GetRowHandleByVisibleIndex(source.VisibleRowCount - 1);

    // You can set ClipboardCopyMode in Xaml.  The point is, we want the
    // headers to be included so we can match the cells up in case the
    // user has reordered them.
    source.ClipboardCopyMode = ClipboardCopyMode.IncludeHeader;

    source.CopyRowsToClipboard(new[] { sourceHandle });

    var clipboardData = Clipboard.GetDataObject();

    var data = clipboardData?.GetData(DataFormats.Text) as string;
    if (data == null)
        return;

    var targetView = target.View as TableView;
    if (targetView == null)
        return;

    targetView.AddNewRow();

    var headersAndRows = data.Split('\n');
    var headers = headersAndRows[0].Split('\t');
    var columnValues = headersAndRows[1].Split('\t');
    var columnLookup = target.Columns.ToDictionary(o => o.HeaderCaption?.ToString());

    for (var i = 0; i < headers.Length; i++)
    {
        var header = headers[i];
        if (columnLookup.TryGetValue(header, out var column))
            target.SetCellValue(targetHandle, column, columnValues[i]);
    }

    // If you want to *move* the row, then uncomment this line to
    // delete the row from the first grid:
    //(source.View as TableView)?.DeleteRow(sourceHandle);
}
 private void dtg_tabletrial_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.D && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {

            DataGridTrialTable ff = new DataGridTrialTable();
            ff.Address = dtgtrialTable.LastOrDefault().Address;
            ff.UserName = dtgtrialTable.LastOrDefault().UserName;
            ff.Phone = dtgtrialTable.LastOrDefault().Phone;
            ff.Surname = dtgtrialTable.LastOrDefault().Surname;

            dtgtrialTable.Add(ff);
        }
    }