在RowHeaderSelect模式下仅复制选定的行,而不会复制未选定的行 [英] Copy only selected rows without not-selected rows between in RowHeaderSelect mode
问题描述
我正在寻找Windows窗体应用程序中C#的可能性,以便能够:
I'm looking for a possibility in C# in Windows Forms Application to be able to:
- 在a处选择多个完整行时间。
- 在选定的行之间包括未选定的行。
- 然后将它们复制到剪贴板,不留空格(留给未选定的行)
DataGridViewSelectionMode.FullRowSelect
不会削减它,因为我也需要能够选择独立的单元格我需要在行复制中启用与 DataGridViewSelectionMode.FullRowSelect
相同的行为,但对于 DataGridViewSelectionMode.RowHeaderSelect
模式。
The DataGridViewSelectionMode.FullRowSelect
doesn't cut it as I also need to be able to select independent cells. I need to enable the same behaviour in row copying as the DataGridViewSelectionMode.FullRowSelect
has but for the DataGridViewSelectionMode.RowHeaderSelect
mode. Would that be possible to do?
谢谢。
推荐答案
首先,要执行此操作,我们将手动从默认复制结果中删除空白行。为此,我们将调用以下方法:
First, to do this we will be manually removing the empty lines from the default copy results. We will call the following method to do so:
private void StripEmptyFromCopy()
{
string[] separated = Clipboard.GetText().Split('\n').Where(s => !String.IsNullOrWhiteSpace(s)).ToArray();
string copy = String.Join("\n", separated);
if (!String.IsNullOrEmpty(copy))
{
Clipboard.SetText(copy);
}
}
解决方案和缺点
-
我最初的想法是通过处理
DataGridView.KeyUp
来做到这一点事件,请检查用户输入的 Ctrl + C :
My initial thought was to do this by handling the
DataGridView.KeyUp
event, checking the user input for Ctrl+C:
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C && (Control.Modifiers & Keys.Control) == Keys.Control)
{
StripEmptyFromCopy();
e.Handled = true;
}
}
- 如果<首先释放kbd> C 。如果先释放 Ctrl ,则空白行仍在剪贴板中。
- Works fine if C is released first. If Ctrl is released first, the empty rows are still in the Clipboard.
创建您的从 DataGridView
继承的自有类并覆盖 ProcessCmdKey
:
Create your own class inherting from DataGridView
and override ProcessCmdKey
:
public class CopyDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.C))
{
StripEmptyFromCopy();
return false;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
- 返回<$ c调用
StripEmptyFromCopy
后,$ c> True 阻止剪贴板获取副本。返回False
可以...但是随后被默认副本覆盖,我不知道在哪里发生。 - Returning
True
after callingStripEmptyFromCopy
prevents the Clipboard from getting the copy. ReturningFalse
works... but then gets overridden by the default copy and I don't know where this occurs.
组合这些想法以捕获 Ctrl + C ,无论哪个键首先在中释放KeyUp
事件处理程序:
Combine these ideas to catch Ctrl+C no matter which key is released first in the KeyUp
event handler:
public class CopyDataGridView : DataGridView
{
public bool Copying { get; set; }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Copying = keyData == (Keys.Control | Keys.C);
return base.ProcessCmdKey(ref msg, keyData);
}
}
// And in the form:
copyDataGridView1.KeyUp += CopyDataGridView1_KeyUp;
private void CopyDataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if (copyDataGridView1.Copying)
{
StripEmptyFromCopy();
copyDataGridView1.Copying = false;
e.Handled = true;
}
}
- 还有更多工作要做,但与选项1相比,结果一致。
这篇关于在RowHeaderSelect模式下仅复制选定的行,而不会复制未选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!