最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

全球信息:DataGridView完美解决复制粘贴功能

来源:博客园


(资料图片)

//在DataGridView的PreviewKeyDown事件中

private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e){if (e.Control && e.KeyCode == Keys.V) // 判断是否按下ctrl+v{Paste(dataGridView1, "", 0, false);//粘贴代码}} 

粘贴,行数和列数不足时,自动添加行数列数。

#region 粘贴public int Paste(DataGridView dgv, string pasteText, int kind, bool b_cut){try{if (kind == 0){pasteText = Clipboard.GetText();}if (string.IsNullOrEmpty(pasteText))return -1;int rowNum = 0;int columnNum = 0;//获得当前剪贴板内容的行、列数for (int i = 0; i < pasteText.Length; i++){if (pasteText.Substring(i, 1) == "\t"){columnNum++;}if (pasteText.Substring(i, 1) == "\n"){rowNum++;}}Object[,] data;//粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\nif (pasteText.Substring(pasteText.Length - 1, 1) == "\n"){rowNum = rowNum - 1;}columnNum = columnNum / (rowNum + 1);data = new object[rowNum + 1, columnNum + 1];String rowStr;//对数组赋值for (int i = 0; i < (rowNum + 1); i++){for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++){rowStr = null;//一行中的最后一列if (colIndex == columnNum && pasteText.IndexOf("\r") != -1){rowStr = pasteText.Substring(0, pasteText.IndexOf("\r"));}//最后一行的最后一列if (colIndex == columnNum && pasteText.IndexOf("\r") == -1){rowStr = pasteText.Substring(0);}//其他行列if (colIndex != columnNum){rowStr = pasteText.Substring(0, pasteText.IndexOf("\t"));pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);}if (rowStr == string.Empty)rowStr = null;data[i, colIndex] = rowStr;}//截取下一行数据pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);}/*检测值是否是列头*//*//获取当前选中单元格所在的列序号int columnindex = dgv.CurrentRow.Cells.IndexOf(dgv.CurrentCell);//获取获取当前选中单元格所在的行序号int rowindex = dgv.CurrentRow.Index;*/int columnindex = -1, rowindex = -1;int columnindextmp = -1, rowindextmp = -1;if (dgv.SelectedCells.Count != 0){columnindextmp = dgv.SelectedCells[0].ColumnIndex;rowindextmp = dgv.SelectedCells[0].RowIndex;}//取到最左上角的 单元格编号foreach (DataGridViewCell cell in dgv.SelectedCells){//dgv.Rows[cell.RowIndex].Selected = true;columnindex = cell.ColumnIndex;if (columnindex > columnindextmp){//交换columnindex = columnindextmp;}elsecolumnindextmp = columnindex;rowindex = cell.RowIndex;if (rowindex > rowindextmp){rowindex = rowindextmp;rowindextmp = rowindex;}elserowindextmp = rowindex;}if (kind == -1){columnindex = 0;rowindex = 0;}//如果行数超过当前列表行数if (rowindex + rowNum + 1 > dgv.RowCount){int mm = rowNum + rowindex + 1 - dgv.RowCount;for (int ii = 0; ii < mm + 1; ii++){dgv.DataBindings.Clear();DataRow row = row = ds.Tables[0].NewRow();ds.Tables[0].Rows.InsertAt(row, ii + rowindex + 1);}}//如果列数超过当前列表列数if (columnindex + columnNum + 1 > dgv.ColumnCount){int mmm = columnNum + columnindex + 1 - dgv.ColumnCount;for (int iii = 0; iii < mmm; iii++){dgv.DataBindings.Clear();DataGridViewTextBoxColumn colum = new DataGridViewTextBoxColumn();dgv.Columns.Insert(columnindex + 1, colum);}}//增加超过的行列for (int j = 0; j < (rowNum + 1); j++){for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++){if (colIndex + columnindex < dgv.Columns.Count){if (dgv.Columns[colIndex + columnindex].CellType.Name == "DataGridViewTextBoxCell"){if (dgv.Rows[j + rowindex].Cells[colIndex + columnindex].ReadOnly == false){dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Value = data[j, colIndex];dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Selected = true;}}}}}//清空剪切板内容if (b_cut)Clipboard.Clear();return 1;}catch{return -1;}}#endregion

关键词: