'DataGridView'에 해당하는 글 2건

dataGridView 만지작 거리면서 내부적으로 copy & paste가 안되는것을 보고 바로 구글링에 들어갔다.

다행히 착한 소스가 보여 바로 들고왔다.

 

디자인은 대략이렇고 구성돼 있고

 

 

소스는 아래와 같다.

 

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace CopyPasteInGrid
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. PopulateDataGridView();
  17. }
  18. public void PopulateDataGridView()
  19. {
  20. dataGridView1.ColumnCount = 6;
  21. dataGridView1.Columns[0].Name = "C0";
  22. dataGridView1.Columns[1].Name = "C1";
  23. dataGridView1.Columns[2].Name = "C2";
  24. dataGridView1.Columns[3].Name = "C3";
  25. dataGridView1.Columns[4].Name = "C4";
  26. dataGridView1.Columns[5].Name = "C5";
  27. dataGridView1.Columns[0].HeaderText = "C0";
  28. dataGridView1.Columns[1].HeaderText = "C1";
  29. dataGridView1.Columns[2].HeaderText = "C2";
  30. dataGridView1.Columns[3].HeaderText = "C3";
  31. dataGridView1.Columns[4].HeaderText = "C4";
  32. dataGridView1.Columns[5].HeaderText = "C5";
  33. dataGridView1.Columns[0].Width = 40;
  34. dataGridView1.Columns[1].Width = 40;
  35. dataGridView1.Columns[2].Width = 40;
  36. dataGridView1.Columns[3].Width = 40;
  37. dataGridView1.Columns[4].Width = 40;
  38. dataGridView1.Columns[5].Width = 40;
  39. dataGridView1.Rows.Add(new string[] { "A1", "B1", "C1", "D1", "E1", "F1" });
  40. dataGridView1.Rows.Add(new string[] { "A2", "B2", "C2", "D2", "E2", "F2" });
  41. dataGridView1.Rows.Add(new string[] { "A3", "B3", "C3", "D3", "E3", "F3" });
  42. dataGridView1.Rows.Add(new string[] { "A4", "B4", "C4", "D4", "E4", "F4" });
  43. dataGridView1.Rows.Add(new string[] { "A5", "B5", "C5", "D5", "E5", "F5" });
  44. dataGridView1.Rows.Add(new string[] { "A6", "B6", "C6", "D6", "E6", "F6" });
  45. dataGridView1.Rows.Add(new string[] { "A7", "B7", "C7", "D7", "E7", "F7" });
  46. dataGridView1.AutoResizeColumns();
  47. }
  48. private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  49. {
  50. if (dataGridView1.SelectedCells.Count > 0)
  51. dataGridView1.ContextMenuStrip = contextMenuStrip1;
  52. }
  53. private void cutToolStripMenuItem_Click(object sender, EventArgs e)
  54. {
  55. //Copy to clipboard
  56. CopyToClipboard();
  57. //Clear selected cells
  58. foreach (DataGridViewCell dgvCell in dataGridView1.SelectedCells)
  59. dgvCell.Value = string.Empty;
  60. }
  61. private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  62. {
  63. CopyToClipboard();
  64. }
  65. private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
  66. {
  67. //Perform paste Operation
  68. PasteClipboardValue();
  69. }
  70. private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
  71. {
  72. try
  73. {
  74. if (e.Modifiers == Keys.Control)
  75. {
  76. switch (e.KeyCode)
  77. {
  78. case Keys.C:
  79. CopyToClipboard();
  80. break;
  81. case Keys.V:
  82. PasteClipboardValue();
  83. break;
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. MessageBox.Show("Copy/paste operation failed. "+ex.Message, "Copy/Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  90. }
  91. }
  92. private void CopyToClipboard()
  93. {
  94. //Copy to clipboard
  95. DataObject dataObj = dataGridView1.GetClipboardContent();
  96. if (dataObj != null)
  97. Clipboard.SetDataObject(dataObj);
  98. }
  99. private void PasteClipboardValue()
  100. {
  101. //Show Error if no cell is selected
  102. if (dataGridView1.SelectedCells.Count == 0)
  103. {
  104. MessageBox.Show("Please select a cell", "Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  105. return;
  106. }
  107. //Get the satring Cell
  108. DataGridViewCell startCell = GetStartCell(dataGridView1);
  109. //Get the clipboard value in a dictionary
  110. Dictionary<int, Dictionary<int, string>> cbValue = ClipBoardValues(Clipboard.GetText());
  111. int iRowIndex = startCell.RowIndex;
  112. foreach (int rowKey in cbValue.Keys)
  113. {
  114. int iColIndex = startCell.ColumnIndex;
  115. foreach (int cellKey in cbValue[rowKey].Keys)
  116. {
  117. //Check if the index is with in the limit
  118. if (iColIndex <= dataGridView1.Columns.Count - 1 && iRowIndex <= dataGridView1.Rows.Count - 1)
  119. {
  120. DataGridViewCell cell = dataGridView1[iColIndex, iRowIndex];
  121. //Copy to selected cells if 'chkPasteToSelectedCells' is checked
  122. if ((chkPasteToSelectedCells.Checked && cell.Selected) ||
  123. (!chkPasteToSelectedCells.Checked))
  124. cell.Value = cbValue[rowKey][cellKey];
  125. }
  126. iColIndex++;
  127. }
  128. iRowIndex++;
  129. }
  130. }
  131. private DataGridViewCell GetStartCell(DataGridView dgView)
  132. {
  133. //get the smallest row,column index
  134. if (dgView.SelectedCells.Count == 0)
  135. return null;
  136. int rowIndex = dgView.Rows.Count - 1;
  137. int colIndex = dgView.Columns.Count - 1;
  138. foreach (DataGridViewCell dgvCell in dgView.SelectedCells)
  139. {
  140. if (dgvCell.RowIndex < rowIndex)
  141. rowIndex = dgvCell.RowIndex;
  142. if (dgvCell.ColumnIndex < colIndex)
  143. colIndex = dgvCell.ColumnIndex;
  144. }
  145. return dgView[colIndex, rowIndex];
  146. }
  147. private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)
  148. {
  149. Dictionary<int, Dictionary<int, string>> copyValues = new Dictionary<int, Dictionary<int, string>>();
  150. String[] lines = clipboardValue.Split('\n');
  151. for (int i = 0; i <= lines.Length - 1; i++)
  152. {
  153. copyValues[i] = new Dictionary<int, string>();
  154. String[] lineContent = lines[i].Split('\t');
  155. //if an empty cell value copied, then set the dictionay with an empty string
  156. //else Set value to dictionary
  157. if (lineContent.Length == 0)
  158. copyValues[i][0] = string.Empty;
  159. else
  160. {
  161. for (int j = 0; j <= lineContent.Length - 1; j++)
  162. copyValues[i][j] = lineContent[j];
  163. }
  164. }
  165. return copyValues;
  166. }
  167. }
  168. }
  •  

     

     

     

    [출처] http://www.codeproject.com/Articles/208281/Copy-Paste-in-Datagridview-Control?rp=/KB/TipsnTricks/CopyPasteInDatagridview/SourceCode.zip

     

     

    - 솔루션 소스 파일 첨부 -

     

    CopyPasteInGrid.zip

     


    WRITTEN BY
    테네시왈츠
    항상 겸손하게 항상 새롭게 항상 진실하게

    ,

    1. 윈도우7 XP 모드 설치하기
    http://pupple.net/130083595056

    2. MS-SQL 2008 설치
    http://blog.naver.com/withyou_ai?Redirect=Log&logNo=120126288830

    3. Sql Developer 에서 MS-SQL 사용하기
    http://blog.naver.com/jeto99?Redirect=Log&logNo=60062317106

    4. 오라클 설치시 javaw.exe 문제로 설치안될때
    http://uiandwe.tistory.com/338

    5. 윈도우 7에 Visual Studio 2005 SP1 설치 후 추가 업데이트
    http://www.microsoft.com/downloads/ko-kr/details.aspx?displaylang=ko&FamilyID=90e2942d-3ad1-4873-a2ee-4acc0aace5b6

    >> 개발관련
    6. DataGridView 활용 Tip
    http://monsterwave.tistory.com/21

    7. MSSQL Linked Server
    http://blog.naver.com/lovzip?Redirect=Log&logNo=20056016860

    8. SqlHelper Transaction
    http://cafe.naver.com/jquerymytour.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=36&


    WRITTEN BY
    테네시왈츠
    항상 겸손하게 항상 새롭게 항상 진실하게

    ,