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

时间:2018-02-12 11:13:58

标签: c# wpf datagrid devexpress devexpress-wpf

我想要做的事情但我作为假人堆叠。这是步骤;

  1. 当用户点击Datagrid上的ctrl + D按钮时
  2. datagrid 值的最后一行将复制到clippopard或某处(例如.CopyToClipboard func。)
  3. 从剪贴板或其他内容再到它可能是一个功能,例如pastToclipboard或我们可以使用InitNewRowEventArgs,它让我们访问 RowHandle 功能
  4. 这是我到目前为止所做的代码。

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

2 个答案:

答案 0 :(得分:1)

我假设'最后一行'表示由当前应用的排序和过滤确定的最后一个可见行(即,滚动到底部时最后显示的行,可能不是在视图中目前)。

有几种方法可以做到这一点,但这个是最简单的(我认为),并且它不需要将您的支持行对象标记为[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);
}

答案 1 :(得分:0)

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

和平

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