Emre
Emre

Reputation: 83

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
  2. The Last row of datagrid values will copied to clippopard or somewhere (eg.CopyToClipboard func.)
  3. From clipboard or something else to again it could be a function e.g. pastToclipboard or we can use InitNewRowEventArgs which it gave us access the RowHandle funciton

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

Upvotes: 0

Views: 377

Answers (2)

Emre
Emre

Reputation: 83

Thanks again to Mike Strobel , but I also included another solution for that. 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);
        }
    }

Upvotes: 0

Mike Strobel
Mike Strobel

Reputation: 25623

I'm assuming that 'last row' means the last visible row as determined by the currently applied sorting and filtering (i.e., 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]:

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

Upvotes: 1

Related Questions