Csharp windowform datagridview Clipboard TO EXCEL OR FROM EXCEL DATA 保存datagridview所有數據
from http://www.codeproject.com/KB/office/DataGridViewCopyPaste.aspx
/// <summary> /// 分行顯示顏色 /// 塗聚文 2011-10-13 /// 締友計算機信息技術有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { //分行顯示顏色 for (int i = 0; i < dataGridView1.RowCount; i++) { if (i % 2 == 0) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightPink; } } } /// <summary> /// 保存dataGridView所有記錄 /// 塗聚文 2011-10-13 /// 締友計算機信息技術有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatoSaveNewItem_Click(object sender, EventArgs e) { int cum = 0; for (int i = 0; i < dataGridView1.RowCount; i++) { administrativeAreaListInfo.AreaCode = dataGridView1[0, i].Value.ToString(); administrativeAreaListInfo.AreaName = dataGridView1[1, i].Value.ToString(); administrativeAreaListInfo.AreaParentID = int.Parse(dataGridView1[2, i].Value.ToString()); int k = administrativeAreaListBLL.InsertAdministrativeAreaList(administrativeAreaListInfo); if (k >= 1) { //MessageBox.Show("oK"); //this.DialogResult = DialogResult.OK; cum = cum + 1; } else { this.DialogResult = DialogResult.Cancel; } } DialogResult dresult = MessageBox.Show("添加:" + cum.ToString() + "條記錄!","信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); if (dresult == DialogResult.OK) { this.Close(); this.DialogResult = DialogResult.OK; } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { // 设定单元格的默认值 e.Row.Cells["AreaParentID"].Value = AreaParentID; e.Row.Cells["AreaParentName"].Value = AreaParentName; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_KeyUp(object sender, KeyEventArgs e) { if ((e.Control && e.KeyCode == Keys.Delete) || (e.Shift && e.KeyCode == Keys.Delete)) { CopyClipboard(); } if ((e.Control && e.KeyCode == Keys.Insert) || (e.Shift && e.KeyCode == Keys.Insert)) { PasteClipboard(); } } /// <summary> /// 復制 /// </summary> private void CopyClipboard() { DataObject d = dataGridView1.GetClipboardContent(); Clipboard.SetDataObject(d); } /// <summary> /// 粘貼 /// </summary> private void PasteClipboard() { try { string s = Clipboard.GetText(); string[] lines = s.Split('\n'); int iFail = 0, iRow = dataGridView1.CurrentCell.RowIndex; int iCol = dataGridView1.CurrentCell.ColumnIndex; DataGridViewCell oCell; foreach (string line in lines) { if (iRow < dataGridView1.RowCount && line.Length > 0) { string[] sCells = line.Split('\t'); for (int i = 0; i < sCells.GetLength(0); ++i) { if (iCol + i < this.dataGridView1.ColumnCount) { oCell = dataGridView1[iCol + i, iRow]; if (!oCell.ReadOnly) { if (oCell.Value.ToString() != sCells[i]) { oCell.Value = Convert.ChangeType(sCells[i], oCell.ValueType); oCell.Style.BackColor = Color.Tomato; } else iFail++;// } } else { break; } } iRow++; } else { break; } if (iFail > 0) MessageBox.Show(string.Format("{0} 更新失敗,因為只讀設置列", iFail)); } } catch (FormatException) { MessageBox.Show("您粘貼錯誤的數據格式為單元格"); return; } } /// <summary> /// 復制到EXCEL /// 塗聚文 2011-10-13 /// 締友計算機信息技術有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void copyToolStripMenuItem_Click(object sender, EventArgs e) { CopyClipboard(); } /// <summary> /// 從EXCEL粘貼 /// 塗聚文 2011-10-13 /// 締友計算機信息技術有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pasteCtrlVToolStripMenuItem_Click(object sender, EventArgs e) { PasteClipboard(); }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)