業務要求:復制:將Excel內容復制到datagridview中
最終效果:復制Excel內容,點擊datagridview中的某個單元格,順着這個單元格自動填充自動增加行。偷懶了,沒寫填充在選擇哪些行就填充到哪些行。
1、添加方法
1 #region 2 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) 3 { 4 //回車新增行 個人筆記 5 switch (keyData) 6 { 7 8 case System.Windows.Forms.Keys.Enter: 9 { 10 11 if (this.Text.IndexOf("訂單") > -1) 12 { 13 try 14 { 15 if (edcol > 0 && edrow == dsMainFilter1.Tables[0].Rows.Count - 1) 16 { 17 18 DataRow row = dsMainFilter1.Tables[0].NewRow(); 19 str = SYSVARS.vars.userId + System.DateTime.Now.ToString("yyyyMMddHHmmss").ToString().Trim() + dsMainFilter1.Tables[0].Rows.Count.ToString();//以時間標識代碼不同的單據號 28 dsMainFilter1.Tables[0].Rows.Add(row); 29 } 30 31 this.dataGridView3.CurrentCell = dataGridView3[3, dsMainFilter1.Tables[0].Rows.Count - 1]; 32 dataGridView3.BeginEdit(true); 33 } 34 catch { } 35 } 36 } 41 42 return true; 43 } 44 45 #region excel復制粘貼功能 46 try 47 { 48 if (this.Text.IndexOf("訂單")>-1) 49 { 50 if (keyData == (Keys.V | Keys.Control)) // ctrl+V 51 { 52 bool bd = dataGridView3.Focus();//避免影響到界面上其他功能使用粘貼 53 if (bd == true) 54 { 55 IDataObject idataObject = Clipboard.GetDataObject(); 56 string da = Clipboard.GetText(); 57 string[] s = idataObject.GetFormats(); 58 copydata(da); 59 return true;//很重要,不寫將會把所有值填充在最后一個單元格里面 60 } 61 62 } 63 } 64 } 65 catch { } 66 #endregion 67 return base.ProcessCmdKey(ref msg, keyData); 68 } 69 #endregion
2、處理剪切板的數據
private void copydata(string data1) { string clipboardText = Clipboard.GetText(); //獲取剪貼板中的內容 if (data1.Trim().Length < 1) { return; } try { int colnum = 0; int rownum = 0; for (int i = 0; i < clipboardText.Length; i++) { if (clipboardText.Substring(i, 1).Equals("\t")) { colnum++; } if (clipboardText.Substring(i, 1).Equals("\n")) { rownum++; } } //粘貼板上的數據來源於EXCEL時,每行末尾都有\n,來源於DataGridView是,最后一行末尾沒有\n if (clipboardText.Substring(clipboardText.Length - 1, 1) == "\n") { rownum--; } colnum = colnum / (rownum + 1); object[,] data; //定義object類型的二維數組 data = new object[rownum + 1, colnum + 1]; //根據剪貼板的行列數實例化數組 string rowStr = ""; //對數組各元素賦值 for (int i = 0; i <= rownum; i++) { for (int j = 0; j <= colnum; j++) { //一行中的其它列 if (j != colnum) { rowStr = clipboardText.Substring(0, clipboardText.IndexOf("\t")); clipboardText = clipboardText.Substring(clipboardText.IndexOf("\t") + 1); } //一行中的最后一列 if (j == colnum && clipboardText.IndexOf("\r") != -1) { rowStr = clipboardText.Substring(0, clipboardText.IndexOf("\r")); } //最后一行的最后一列 if (j == colnum && clipboardText.IndexOf("\r") == -1) { rowStr = clipboardText.Substring(0); } data[i, j] = rowStr; } //截取下一行及以后的數據 clipboardText = clipboardText.Substring(clipboardText.IndexOf("\n") + 1); } clipboardText = Clipboard.GetText(); int start, end = -1, index, rowStart = 0, columnStart = 0; rowStart = r;//選中單元格的行號 columnStart = Icol;//選中單元格的列號 for (int i = 0; i <=rownum; i++) { #region 如果datagridview中行數不夠,就自動增加行 if ((i + rowStart) > dataGridView3.Rows.Count - 1) { //添加新行
DataRow row = dsMainFilter1.Tables[0].NewRow(); str = SYSVARS.vars.userId + System.DateTime.Now.ToString("yyyyMMddHHmmss").ToString().Trim() + dsMainFilter1.Tables[0].Rows.Count.ToString();//以時間標識代碼不同的單據號 dsMainFilter1.Tables[0].Rows.Add(row); } #endregion for (int j = 0; j <= colnum; j++)//將值賦值過去---如果datagridview中沒有自動增加列 { #region 需要判斷單元格是不是只讀的,是只讀的就不用不賦值 bool iszd = this.dataGridView3.Rows[i + rowStart].Cells[j + columnStart].ReadOnly; if (iszd == true) { continue; } #endregion string sjz = ""; try { sjz = data[i, j].ToString(); } catch { sjz = ""; } if (sjz.Trim().Length < 1) { continue; }//直接復制this.dataGridView3.Rows[i + rowStart].Cells[j + columnStart].Value = sjz; } } } catch { } }