简体   繁体   English

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

[英]WPF Datagrid copy last row of grid to new row in grid (Devexpress)

there is thing that I want to do but I stack as a dummy. 有一些我想做的事情,但我却是一个假人。 Here is the steps; 步骤如下;

  1. When user hit the ctrl + D button on Datagrid 当用户点击Datagrid上的ctrl + D按钮时
  2. The Last row of datagrid values will copied to clippopard or somewhere (eg.CopyToClipboard func.) datagrid最后一行值将复制到clippopard或某个地方(例如,CopyToClipboard函数)。
  3. From clipboard or something else to again it could be a function eg pastToclipboard or we can use InitNewRowEventArgs which it gave us access the RowHandle funciton 从剪贴板或其他东西,它可能是一个函数,例如pastToclipboard,或者我们可以使用InitNewRowEventArgs,它使我们可以访问RowHandle功能

Here is code I done so far. 这是我到目前为止完成的代码。

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");   
    }

I'm assuming that 'last row' means the last visible row as determined by the currently applied sorting and filtering (ie, the row that would appear last when you scroll to the bottom, which may not be in view at the moment). 我假设“最后一行”是指由当前应用的排序和过滤所确定的最后一个可见行(即,当您滚动到底部时可能会最后出现的行,此行目前可能不在视图中)。

There's a couple ways to do this, but this one is the simplest (I think), and it doesn't require your backing row objects to be marked [Serializable] : 有两种方法可以做到这一点,但这是最简单的方法(我认为),并且不需要将后备行对象标记为[Serializable]

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);
}

Thanks again to Mike Strobel , but I also included another solution for that. 再次感谢Mike Strobel,但我还提供了另一种解决方案。 I'm writing down here to someone who will need it. 我在这里写信给需要它的人。

Peace 和平

 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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM