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);
}
}
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控件在编辑单元格时事件触发顺序
- winform datagridview设置列单元格不可编辑可以选定指定内容
- winform datagridview 根据条件来判断某一个单元格的值是否为可编辑
- WinForm - 格式化DataGridView单元格数据
- winForm开发问题,vs的bug,Datagridview始终不能编辑!
- datagridview 单元格为 编辑状态
- 捕捉DataGridView的单元格编辑时的键事件
- Winform datagridview 设置单元格为只读属性
- C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)
- DataGridView指定单元格的编辑状态与修改
- datagridview编辑列的颜色,设置点击datagridview当前单元格事件
- DataGridView鼠标的拖放操作及单元格编辑结束数据复制与单元格单击数据粘贴及两个DataGridView相互拖放操作
- [WinForm]DataGridView选中一行获取指定单元格的值
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- [WinForm]DataGridView选中一行获取指定单元格的值
- winform中DatagridView单元格动态绑定控件
- [网上搜集]c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- 在winform中datagridview在设计器中设置了编辑列,有一列是datagridviewcomboBoxColumn,如何给这列赋