WPF Datagrid 将最后一行网格复制到网格中的新行(Devexpress)
WPF Datagrid copy last row of grid to new row in grid (Devexpress)
有件事我想做,但我堆成一个假人。这是步骤;
- 当用户在 Datagrid 上按下 ctrl + D 按钮时
- datagrid 值的最后一行 将复制到 clippopard 或某处(eg.CopyToClipboard 函数)
- 从剪贴板或其他东西到再次它可能是一个函数,例如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");
}
我假设 'last row' 表示由当前应用的排序和过滤确定的最后 visible 行(即,当您滚动到底部,目前可能不在视图中。
有几种方法可以做到这一点,但这是最简单的(我认为),它不需要标记你的支持行对象 [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);
}
再次感谢 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);
}
}
有件事我想做,但我堆成一个假人。这是步骤;
- 当用户在 Datagrid 上按下 ctrl + D 按钮时
- datagrid 值的最后一行 将复制到 clippopard 或某处(eg.CopyToClipboard 函数)
- 从剪贴板或其他东西到再次它可能是一个函数,例如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");
}
我假设 'last row' 表示由当前应用的排序和过滤确定的最后 visible 行(即,当您滚动到底部,目前可能不在视图中。
有几种方法可以做到这一点,但这是最简单的(我认为),它不需要标记你的支持行对象 [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);
}
再次感谢 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);
}
}