C#系統默認情況下不支持批量粘貼多行或多個單元格數據,但是有時想直接複製dataGridView的一部分數據,然後一起粘貼到DatagridView的指定位置。
關鍵步驟
0.創建一個C#窗體應用程序this,調用一個DataGridView控件this.dataGridView1
1.重寫this.dataGridView1的ProcessCmdKey方法,獲取鍵盤點擊事件,識別Ctrl+V
2.獲取剪貼板HTML數據並解析,將多行多列數據分別填至光標指定或選中的單元格中
3.清空消息參數,防止系統調用默認的剪貼功能,將複製的第一行數據不正確地填充至一個單元格中
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//在檢測到按Ctrl+V鍵後,系統無法複製多單元格數據,重寫ProcessCmdKey方法,屏蔽系統粘貼事件,使用自定義粘貼事件,在事件中對剪貼板的HTML格式進行處理,獲取表數據,更新DataGrid控件內容
if (keyData == (Keys.V| Keys.Control)) // &&
{
IDataObject idataObject = Clipboard.GetDataObject();
string[] s = idataObject.GetFormats();
string data;
if (!s.Any(f=>f=="OEMText"))
{
if (!s.Any(f=>f=="HTML Format"))
{
}
else
{
data = idataObject.GetData("HTML Format").ToString();//多個單元格
copyClipboardHtmltoGrid(data);
//msg = Message.;
msg = new Message();
return base.ProcessCmdKey(ref msg, Keys.Control);
}
}else
data = idataObject.GetData("OEMText").ToString();//單個單元格,使用系統功能,無需處理
}
return base.ProcessCmdKey(ref msg, keyData);
}
當複製單行數據時,剪貼板中沒有HTML格式的數據,此時可獲取OEMText格式的數據,或者不加處理,使用系統粘貼功能;
當複製多行時,可以通過解析剪貼板的HTML格式的數據,獲取多個單元格的數據
關於Windows剪貼板數據格式的問題,可以參考我的上一篇博客
copyClipboardHtmltoGrid(data);方法識別剪貼板的HTML格式數據,並將結果動態地賦值給this.dataGridView1
private void copyClipboardHtmltoGrid(string data)
{
//截取出HTML內容
int start, end = -1, index, rowStart = 0, columnStart = 0;
Regex regex = new Regex(@"StartFragment:\d+");
Match match = regex.Match(data);
if (match.Success)
{
start = Convert.ToInt16(match.Value.Substring(14));
}
else
{
return;
}
regex = new Regex(@"EndFragment:\d+");
match = regex.Match(data, match.Index + match.Length);
if (match.Success)
{
end = Convert.ToInt16(match.Value.Substring(12));
}
else
{
return;
}
if (this.dataGridView1.SelectedCells.Count > 0)
{
rowStart = this.dataGridView1.SelectedCells[0].RowIndex;
columnStart = this.dataGridView1.SelectedCells[0].ColumnIndex;
}
data = data.Substring(start, end - start);
MatchCollection matchcollection = new Regex(@"<TR>[\S\s]*?</TR>").Matches(data), sub_matchcollection;
int count = rowStart + matchcollection.Count - this.dataGridView1.RowCount;
if (count>=0)
{
this.dataGridView1.Rows.Add(count+1);
}
for (int i = 0; i < matchcollection.Count && i + rowStart < this.dataGridView1.RowCount ; i++)
{
sub_matchcollection = new Regex(@"<TD>[\S\s]*?</TD>").Matches(matchcollection[i].Value);
for (int j = 0; j < sub_matchcollection.Count && j + columnStart < this.dataGridView1.ColumnCount; j++)
{
this.dataGridView1.Rows[i + rowStart].Cells[j + columnStart].Value = sub_matchcollection[j].Value.Substring(4, sub_matchcollection[j].Length - 9).Trim();
}
}
}
效果截圖: