datagridview粘貼,,當複製總行列數超過datagridview行列數時,要能夠自動增加行與列:
見代碼:
#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內複製時,最後一行末沒有\n if (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; } else columnindextmp = columnindex; rowindex = cell.RowIndex; if (rowindex > rowindextmp) { rowindex = rowindextmp; rowindextmp = rowindex; } else rowindextmp = 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