dataGridView 만지작 거리면서 내부적으로 copy & paste가 안되는것을 보고 바로 구글링에 들어갔다.
다행히 착한 소스가 보여 바로 들고왔다.
디자인은 대략이렇고 구성돼 있고
소스는 아래와 같다.
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
namespace CopyPasteInGrid
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
PopulateDataGridView();
-
}
-
public void PopulateDataGridView()
-
{
-
dataGridView1.ColumnCount = 6;
-
dataGridView1.Columns[0].Name = "C0";
-
dataGridView1.Columns[1].Name = "C1";
-
dataGridView1.Columns[2].Name = "C2";
-
dataGridView1.Columns[3].Name = "C3";
-
dataGridView1.Columns[4].Name = "C4";
-
dataGridView1.Columns[5].Name = "C5";
-
dataGridView1.Columns[0].HeaderText = "C0";
-
dataGridView1.Columns[1].HeaderText = "C1";
-
dataGridView1.Columns[2].HeaderText = "C2";
-
dataGridView1.Columns[3].HeaderText = "C3";
-
dataGridView1.Columns[4].HeaderText = "C4";
-
dataGridView1.Columns[5].HeaderText = "C5";
-
dataGridView1.Columns[0].Width = 40;
-
dataGridView1.Columns[1].Width = 40;
-
dataGridView1.Columns[2].Width = 40;
-
dataGridView1.Columns[3].Width = 40;
-
dataGridView1.Columns[4].Width = 40;
-
dataGridView1.Columns[5].Width = 40;
-
dataGridView1.AutoResizeColumns();
-
}
-
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
-
{
-
if (dataGridView1.SelectedCells.Count > 0)
-
dataGridView1.ContextMenuStrip = contextMenuStrip1;
-
}
-
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
-
{
-
//Copy to clipboard
-
CopyToClipboard();
-
//Clear selected cells
-
foreach (DataGridViewCell dgvCell in dataGridView1.SelectedCells)
-
dgvCell.Value = string.Empty;
-
}
-
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
-
{
-
CopyToClipboard();
-
}
-
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
-
{
-
//Perform paste Operation
-
PasteClipboardValue();
-
}
-
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
-
{
-
try
-
{
-
if (e.Modifiers == Keys.Control)
-
{
-
switch (e.KeyCode)
-
{
-
case Keys.C:
-
CopyToClipboard();
-
break;
-
case Keys.V:
-
PasteClipboardValue();
-
break;
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show("Copy/paste operation failed. "+ex.Message, "Copy/Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
-
}
-
}
-
private void CopyToClipboard()
-
{
-
//Copy to clipboard
-
DataObject dataObj = dataGridView1.GetClipboardContent();
-
if (dataObj != null)
-
Clipboard.SetDataObject(dataObj);
-
}
-
private void PasteClipboardValue()
-
{
-
//Show Error if no cell is selected
-
if (dataGridView1.SelectedCells.Count == 0)
-
{
-
MessageBox.Show("Please select a cell", "Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
-
return;
-
}
-
//Get the satring Cell
-
DataGridViewCell startCell = GetStartCell(dataGridView1);
-
//Get the clipboard value in a dictionary
-
Dictionary<int, Dictionary<int, string>> cbValue = ClipBoardValues(Clipboard.GetText());
-
int iRowIndex = startCell.RowIndex;
-
foreach (int rowKey in cbValue.Keys)
-
{
-
int iColIndex = startCell.ColumnIndex;
-
foreach (int cellKey in cbValue[rowKey].Keys)
-
{
-
//Check if the index is with in the limit
-
if (iColIndex <= dataGridView1.Columns.Count - 1 && iRowIndex <= dataGridView1.Rows.Count - 1)
-
{
-
DataGridViewCell cell = dataGridView1[iColIndex, iRowIndex];
-
//Copy to selected cells if 'chkPasteToSelectedCells' is checked
-
cell.Value = cbValue[rowKey][cellKey];
-
}
-
iColIndex++;
-
}
-
iRowIndex++;
-
}
-
}
-
private DataGridViewCell GetStartCell(DataGridView dgView)
-
{
-
//get the smallest row,column index
-
if (dgView.SelectedCells.Count == 0)
-
return null;
-
int rowIndex = dgView.Rows.Count - 1;
-
int colIndex = dgView.Columns.Count - 1;
-
foreach (DataGridViewCell dgvCell in dgView.SelectedCells)
-
{
-
if (dgvCell.RowIndex < rowIndex)
-
rowIndex = dgvCell.RowIndex;
-
if (dgvCell.ColumnIndex < colIndex)
-
colIndex = dgvCell.ColumnIndex;
-
}
-
return dgView[colIndex, rowIndex];
-
}
-
private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)
-
{
-
Dictionary<int, Dictionary<int, string>> copyValues = new Dictionary<int, Dictionary<int, string>>();
-
String[] lines = clipboardValue.Split('\n');
-
for (int i = 0; i <= lines.Length - 1; i++)
-
{
-
String[] lineContent = lines[i].Split('\t');
-
//if an empty cell value copied, then set the dictionay with an empty string
-
//else Set value to dictionary
-
if (lineContent.Length == 0)
-
copyValues[i][0] = string.Empty;
-
else
-
{
-
for (int j = 0; j <= lineContent.Length - 1; j++)
-
copyValues[i][j] = lineContent[j];
-
}
-
}
-
return copyValues;
-
}
-
}
-
}
- 솔루션 소스 파일 첨부 -
'020. Prigraming > 01. C#' 카테고리의 다른 글
[C#] DataGridView Row 이동처리 (0) | 2012.09.05 |
---|---|
[C#] XML 간편접근 (0) | 2012.07.09 |
[C#] DataGridView - Related Contents (0) | 2012.06.04 |
[C#] Dataset 데이터 내부 데이터 삭제할 경우(추가) (0) | 2012.05.08 |
[C#] 쓰레드 사용시 Invoke 처리 (0) | 2012.05.03 |
WRITTEN BY
- 테네시왈츠
항상 겸손하게 항상 새롭게 항상 진실하게
,