您的位置:首页 > 运维架构

WinForm DataGridView 可编辑单元格拷贝

2017-09-14 00:44 423 查看
1.Form1_Load中绑定数据源

DataTable Data = new DataTable();

Data.Columns.Add("id",typeof(int));

……

//设为false,否则ColId.Index 总是0

dgSource.AutoGenerateColumns = false;

dgSource.DataSource = Data;

dgSource.AllowUserToAddRows = true;

2.添加DataError事件

private void dgSource_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
2.KeyDown事件处理

//拷贝
if (e.Control && e.KeyCode == Keys.C)
{

//单个单元格使用系统默认行为
if (dgSource.SelectedCells.Count == 0 || dgSource.SelectedCells.Count == 1) return;

Dictionary<string, Dictionary<string, object>> data = new Dictionary<string, Dictionary<string, object>>();

string rkey = null;
foreach (DataGridViewCell cell in dgSource.SelectedCells)
{
rkey = cell.RowIndex.ToString();
if (!data.ContainsKey(rkey))
{
data.Add(rkey, new Dictionary<string, object>());
}

data[rkey][cell.OwningColumn.DataPropertyName] = cell.Value;
}
//行排一下序
data = data.OrderBy(d => int.Parse(d.Key)).ToDictionary(d => d.Key, d => d.Value);
//保存为json格式
Clipboard.SetText(Newtonsoft.Json.JsonConvert.SerializeObject(data));
e.Handled = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
string text = Clipboard.GetText();

if (dgSource.SelectedCells.Count == 0 || string.IsNullOrEmpty(text)) return;

//拷贝
if (dgSource.SelectedCells.Count == 1) {
try
{
dgSource.BeginEdit(false);
dgSource.CurrentCell.Value = text;
//如果是最后一行,会自动新增一行
dgSource.NotifyCurrentCellDirty(true);
}
finally
{
dgSource.EndEdit();
}
e.Handled = true;
return;
}

int columnIndex = -1;
bool isNewRow = false;

List<DataGridViewCell> list = null;
Dictionary<int, List<DataGridViewCell>> cells = new Dictionary<int, List<DataGridViewCell>>();
foreach (DataGridViewCell cell in dgSource.SelectedCells)
{
if (!cells.ContainsKey(cell.RowIndex))
{
if (columnIndex == -1 && cell.RowIndex == dgSource.NewRowIndex)
{
columnIndex = cell.ColumnIndex;
isNewRow = true;
}

if (list != null)
{
list.Sort((a, b) => a.ColumnIndex.CompareTo(b.ColumnIndex));
}
list = new List<DataGridViewCell>();
cells.Add(cell.RowIndex, list);
}
cells[cell.RowIndex].Add(cell);
}

list.Sort((a, b) => a.ColumnIndex.CompareTo(b.ColumnIndex));

cells = cells.OrderBy(k => k.Key).ToDictionary(k => k.Key, k => k.Value);

Dictionary<string, Dictionary<string, object>> data = null;

try
{
//本程序的拷贝数据 json格式
data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(text);
if (data != null)
{
string[] rows = data.Keys.ToArray();
int[] selectedRows = cells.Keys.ToArray();
for (int i = 0, j = 0; i < rows.Length && i < selectedRows.Length && j < cells[selectedRows[i]].Count;)
{
if (columnIndex == -1 && cells[selectedRows[i]][j].RowIndex == dgSource.NewRowIndex)
{
columnIndex = cells[selectedRows[i]][j].ColumnIndex;
isNewRow = true;
}

if (data[rows[i]].ContainsKey(cells[selectedRows[i]][j].OwningColumn.DataPropertyName))
{
cells[selectedRows[i]][j].Value = data[rows[i]][cells[selectedRows[i]][j].OwningColumn.DataPropertyName];
}
j++;
if (j == cells[selectedRows[i]].Count)
{
j = 0;
i++;
}
}

e.Handled = true;
}
}
catch
{
//表格拷贝数据
string[] strs = text.Split(new string[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.None);
string[] cls = null;

int[] rows = cells.Keys.ToArray();

for (int i = 0; i < strs.Length && i < rows.Length;i++)
{
cls = strs[i].Split('\t');
for (int j = 0; j < cells[rows[i]].Count && j < cls.Length; j++)
{
cells[rows[i]][j].Value = cls[j];
}
}
e.Handled = true;
}

if (isNewRow)
{
dgSource.CurrentCell = dgSource.Rows[dgSource.NewRowIndex].Cells[columnIndex];
//切换当前单元格为最后一行的某编辑单元格,切换编辑状态,可以使最后一行继续添加一行
dgSource.BeginEdit(false);
dgSource.EndEdit();
dgSource.NotifyCurrentCellDirty(true);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WinForm DataGridView Copy
相关文章推荐