■ Clipboard 클래스에서 엑셀 데이터를 붙여넣는 방법을 보여준다.
▶ PasteCSVForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
using System; using System.ComponentModel; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO; using System.Reflection; using System.Text; using System.Windows.Forms; namespace TestProject { /// <summary> /// CSV 붙여넣기 폼 /// </summary> public partial class PasteCSVForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 단순 대리자 - SimpleDelegate() /// <summary> /// 단순 대리자 /// </summary> private delegate void SimpleDelegate(); #endregion #region 진행 패널 리셋하기 대리자 - ResetProgressPanelDelegate(minimum, maximum) /// <summary> /// 진행 패널 리셋하기 대리자 /// </summary> /// <param name="minimum">최소값</param> /// <param name="maximum">최대값</param> private delegate void ResetProgressPanelDelegate(int minimum, int maximum); #endregion #region 진행 패널 설정하기 대리자 - SetProgressPanelDelegate(message, progressValue) /// <summary> /// 진행 패널 설정하기 대리자 /// </summary> /// <param name="message">메시지</param> /// <param name="value">값</param> private delegate void SetProgressPanelDelegate(string message, int value); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 헤더 텍스트 사용 여부 /// </summary> private bool useHeaderText = false; /// <summary> /// 실제 헤더 텍스트 사용 여부 /// </summary> private bool actualUseHeaderText = false; /// <summary> /// 최대 컬럼 카운트 /// </summary> private int maximumColumnCount = -1; /// <summary> /// 실제 컬럼 카운트 /// </summary> private int actualColumnCount = -1; /// <summary> /// 소스 데이터 객체 /// </summary> private IDataObject sourceDataObject = null; /// <summary> /// 소스 텍스트 /// </summary> private string sourceText = null; /// <summary> /// 소스 테이블 /// </summary> private DataTable sourceTable = null; /// <summary> /// 소스 리더 /// </summary> private IDataReader sourceReader = null; /// <summary> /// 자동 컬럼 크기 허용 여부 /// </summary> private bool allowAutoColumnSizing = false; /// <summary> /// 자동 행 크기 허용 여부 /// </summary> private bool allowAutoRowSizing = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 헤더 텍스트 사용 여부 - UseHeaderText /// <summary> /// 헤더 텍스트 사용 여부 /// </summary> public bool UseHeaderText { get { return this.useHeaderText; } set { this.useHeaderText = value; } } #endregion #region 최대 컬럼 카운트 - MaximumColumnCount /// <summary> /// 최대 컬럼 카운트 /// </summary> public int MaximumColumnCount { get { return this.maximumColumnCount; } set { this.maximumColumnCount = value; } } #endregion #region 자동 컬럼 크기 허용 여부 - AllowAutoColumnSizing /// <summary> /// 자동 컬럼 크기 허용 여부 /// </summary> public bool AllowAutoColumnSizing { get { return this.allowAutoColumnSizing; } set { this.allowAutoColumnSizing = value; } } #endregion #region 자동 행 크기 허용 여부 - AllowAutoRowSizing /// <summary> /// 자동 행 크기 허용 여부 /// </summary> public bool AllowAutoRowSizing { get { return this.allowAutoRowSizing; } set { this.allowAutoRowSizing = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PasteCSVForm() /// <summary> /// 생성자 /// </summary> public PasteCSVForm() { InitializeComponent(); this.dataGridView.ReadOnly = true; this.dataGridView.RowHeadersVisible = false; this.dataGridView.AllowUserToAddRows = false; this.dataGridView.AllowUserToDeleteRows = false; this.dataGridView.AllowUserToOrderColumns = false; this.dataGridView.AllowUserToResizeColumns = true; this.dataGridView.AllowUserToResizeRows = true; this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.dataGridView.MultiSelect = true; SetDoubleBuffered(this.dataGridView, true); GotFocus += Form_GotFocus; this.dataGridView.KeyDown += dataGridView_KeyDown; this.okButton.Click += okButton_Click; this.cancelButton.Click += cancelButton_Click; this.backgroundWorker.DoWork += backgroundWorker_DoWork; this.backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; this.deleteSelectedRowsMenuItem.Click += deleteSelectedRowsMenuItem_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 테이블 구하기 - GetDataTable() /// <summary> /// 데이터 테이블 구하기 /// </summary> /// <returns>데이터 테이블</returns> public DataTable GetDataTable() { return this.sourceTable; } #endregion #region 데이터 리더 구하기 - GetDataReader() /// <summary> /// 데이터 리더 구하기 /// </summary> /// <returns>데이터 리더</returns> public IDataReader GetDataReader() { if(this.sourceTable == null || this.sourceTable.Rows.Count == 0) { return null; } if(this.sourceReader == null) { this.sourceReader = new DataTableReader(this.sourceTable); } return this.sourceReader; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 포커스 획득시 처리하기 - Form_GotFocus(sender, e) /// <summary> /// 폼 포커스 획득시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_GotFocus(object sender, EventArgs e) { this.dataGridView.Focus(); } #endregion #region 데이터 그리드 뷰 키 DOWN 처리하기 - dataGridView_KeyDown(sender, e) /// <summary> /// 데이터 그리드 뷰 키 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridView_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.V && e.Modifiers == Keys.Control) { if(Clipboard.ContainsData(DataFormats.CommaSeparatedValue)) { ClearData(); this.sourceDataObject = Clipboard.GetDataObject(); SetData(); } } else if(e.KeyCode == Keys.Delete) { ProcessDeleteSelectedRows(); } } #endregion #region 백그라운드 작업자 작업시 처리하기 - backgroundWorker_DoWork(sender, e) /// <summary> /// 백그라운드 작업자 작업시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { try { ResetProgressPanel(0, 100); SetProgressPanel("Pasting...", 0); ShowProgressPanel(); #region 소스 텍스트를 설정한다. if(this.sourceDataObject == null) { return; } Stream stream = this.sourceDataObject.GetData(DataFormats.CommaSeparatedValue) as Stream; if(stream == null) { return; } StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("EUC-KR")); if(streamReader == null) { return; } string sourceText = streamReader.ReadToEnd().Replace("\0", string.Empty).TrimEnd('\n').TrimEnd('\r'); if(sourceText == null) { this.sourceText = null; } else { sourceText = sourceText.Trim(); if(sourceText.Length == 0) { this.sourceText = null; } else { this.sourceText = sourceText; } } if(this.sourceText == null) { return; } #endregion SetProgressPanel("Pasting...", 20); #region 소스 리스트를 설정한다. List<string[]> sourceList = GetSourceList(this.sourceText); if(sourceList == null || sourceList.Count == 0) { return; } #endregion SetProgressPanel("Pasting...", 40); #region 소스 컬럼 카운트를 설정한다. int sourceColumnCount = GetSourceColumnCount(sourceList); if(sourceColumnCount < 1) { return; } #endregion #region 실제 컬럼 카운트를 설정한다. this.actualColumnCount = GetActualColumnCount(this.maximumColumnCount, sourceColumnCount); if(this.actualColumnCount < 1) { return; } #endregion #region 실제 헤더 텍스트 사용 여부를 설정한다. if(this.useHeaderText) { this.actualUseHeaderText = ValidateColumnName(sourceList[0], this.actualColumnCount); } else { this.actualUseHeaderText = false; } #endregion SetProgressPanel("Pasting...", 50); #region 소스 테이블을 설정한다. this.sourceTable = GetSourceTable(sourceList, this.actualUseHeaderText, this.actualColumnCount); if(this.sourceTable == null) { return; } #endregion SetProgressPanel("Pasting...", 70); SetDataGridDataSource(); SetProgressPanel("Pasting...", 100); System.Threading.Thread.Sleep(500); } catch { ClearData(); } finally { HideProgressPanel(); } } #endregion #region 백그라운드 작업자 작업자 완료시 처리하기 - backgroundWorker_RunWorkerCompleted(sender, e) /// <summary> /// 백그라운드 작업자 작업자 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.okButton.Enabled = true; this.cancelButton.Enabled = true; } #endregion #region OK 버튼 클릭시 처리하기 - okButton_Click(sender, e) /// <summary> /// OK 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void okButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.OK; Close(); } #endregion #region Cancel 버튼 클릭시 처리하기 - cancelButton_Click(sender, e) /// <summary> /// Cancel 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } #endregion #region Delete Selected Rows 메뉴 항목 클릭시 처리하기 - deleteSelectedRowsMenuItem_Click(sender, e) /// <summary> /// Delete Selected Rows 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void deleteSelectedRowsMenuItem_Click(object sender, EventArgs e) { ProcessDeleteSelectedRows(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 소스 리스트 구하기 - GetSourceList(sourceText) /// <summary> /// 소스 리스트 구하기 /// </summary> /// <param name="sourceText">소스 텍스트</param> /// <returns>소스 리스트</returns> private List<string[]> GetSourceList(string sourceText) { //if(sourceText == null || sourceText.Length == 0) //{ // return null; //} //string[] sourceLineArray = sourceText.Split('\n'); //int sourceLineCount = sourceLineArray.Length; //List<string[]> sourceList = new List<string[]>(); //for(int i = 0; i < sourceLineCount; i++) //{ // string line = sourceLineArray[i].TrimEnd('\r'); // if(line == "\0") // { // continue; // } // string[] elementArray = line.Split(','); // sourceList.Add(elementArray); //} //return sourceList; if(sourceText == null || sourceText.Length == 0) { return null; } string[] sourceLineArray = sourceText.Split(new string[] { "\r\n" }, StringSplitOptions.None); int sourceLineCount = sourceLineArray.Length; List<string[]> sourceList = new List<string[]>(); for(int i = 0; i < sourceLineCount; i++) { string line = sourceLineArray[i]; if(line == "\0") { continue; } string[] elementArray = line.Split(','); int elementCount = elementArray.Length; for(int j = 0; j < elementCount; j++) { string element = elementArray[j]; //element = element.Replace("\n", "\r\n"); if(element.Contains("\n")) { element = element.TrimStart('\"').TrimEnd('\"'); } elementArray[j] = element; } sourceList.Add(elementArray); } return sourceList; } #endregion #region 소스 컬럼 카운트 구하기 - GetSourceColumnCount(sourceList) /// <summary> /// 소스 컬럼 카운트 구하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <returns>소스 컬럼 카운트</returns> private int GetSourceColumnCount(List<string[]> sourceList) { int maximumColumnCount = int.MinValue; foreach(string[] sourceArray in sourceList) { int columnCount = sourceArray.Length; if(columnCount > maximumColumnCount) { maximumColumnCount = columnCount; } } return maximumColumnCount; } #endregion #region 실제 컬럼 카운트 구하기 - GetActualColumnCount(maximumColumnCount, sourceColumnCount) /// <summary> /// 실제 컬럼 카운트 구하기 /// </summary> /// <param name="maximumColumnCount">최대 컬럼 카운트</param> /// <param name="sourceColumnCount">소스 컬럼 카운트</param> /// <returns>실제 컬럼 카운트</returns> private int GetActualColumnCount(int maximumColumnCount, int sourceColumnCount) { int actualColumnCount = 0; if(maximumColumnCount == -1) { actualColumnCount = sourceColumnCount; } else { if(maximumColumnCount > sourceColumnCount) { actualColumnCount = sourceColumnCount; } else { actualColumnCount = maximumColumnCount; } } return actualColumnCount; } #endregion #region 컬럼명 무결성 조사하기 - ValidateColumnName(columnNameArray, actualColumnCount) /// <summary> /// 컬럼명 무결성 조사하기 /// </summary> /// <param name="columnNameArray">컬럼명 배열</param> /// <param name="actualColumnCount">실제 컬럼 카운트</param> /// <returns>컬럼명 무결성 조사 결과</returns> private bool ValidateColumnName(string[] columnNameArray, int actualColumnCount) { if(columnNameArray.Length < actualColumnCount) { return false; } Dictionary<string, string> dictionary = new Dictionary<string, string>(); foreach(string columnName in columnNameArray) { if(dictionary.ContainsKey(columnName)) { return false; } else { dictionary.Add(columnName, columnName); } } return true; } #endregion #region 비어있는 소스 테이블 구하기 - GetEmptySourceTable(sourceList, actualUseHeaderText, actualColumnCount) /// <summary> /// 비어있는 소스 테이블 구하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <param name="actualUseHeaderText">실제 헤더 텍스트 사용 여부</param> /// <param name="actualColumnCount">실제 컬럼 카운트</param> /// <returns>비어있는 소스 테이블</returns> private DataTable GetEmptySourceTable(List<string[]> sourceList, bool actualUseHeaderText, int actualColumnCount) { DataTable sourceTable = new DataTable(); if(actualUseHeaderText) { for(int i = 0; i < actualColumnCount; i++) { string columnName = sourceList[0][i]; sourceTable.Columns.Add(columnName, typeof(string)); } } else { for(int i = 0; i < actualColumnCount; i++) { string columnName = string.Format("Column{0}", i + 1); sourceTable.Columns.Add(columnName, typeof(string)); } } return sourceTable; } #endregion #region 소스 테이블 채우기 - FillSourceTable(sourceTable, sourceList, actualUseHeaderText, actualColumnCount) /// <summary> /// 소스 테이블 채우기 /// </summary> /// <param name="sourceTable">소스 테이블</param> /// <param name="sourceList">소스 리스트</param> /// <param name="actualUseHeaderText">실제 헤더 텍스트 사용 여부</param> /// <param name="actualColumnCount">실제 컬럼 카운트</param> private void FillSourceTable(DataTable sourceTable, List<string[]> sourceList, bool actualUseHeaderText, int actualColumnCount) { int startRow = actualUseHeaderText ? 1 : 0; for(int i = startRow; i < sourceList.Count; i++) { DataRow row = sourceTable.NewRow(); for(int j = 0; j < actualColumnCount; j++) { try { row[j] = sourceList[i][j]; } catch { } } sourceTable.Rows.Add(row); } } #endregion #region 소스 테이블 구하기 - GetSourceTable(sourceList, actualUseHeaderText, actualColumnCount) /// <summary> /// 소스 테이블 구하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <param name="actualUseHeaderText">실제 헤더 텍스트 사용 여부</param> /// <param name="actualColumnCount">실제 컬럼 카운트</param> /// <returns>소스 테이블</returns> private DataTable GetSourceTable(List<string[]> sourceList, bool actualUseHeaderText, int actualColumnCount) { if(sourceList == null || sourceList.Count == 0) { return null; } DataTable sourceTable = GetEmptySourceTable(sourceList, actualUseHeaderText, actualColumnCount); FillSourceTable(sourceTable, sourceList, actualUseHeaderText, actualColumnCount); return sourceTable; } #endregion #region 진행 패널 보여주기 - ShowProgressPanel() /// <summary> /// 진행 패널 보여주기 /// </summary> private void ShowProgressPanel() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(ShowProgressPanel); this.dataGridView.Invoke(simpleDelegate); } else { this.progressPanel.Location = new Point ( (ClientRectangle.Width - this.progressPanel.Width ) / 2, (ClientRectangle.Height - this.progressPanel.Height) / 2 ); this.progressPanel.BringToFront(); this.progressPanel.Visible = true; } } #endregion #region 진행 패널 숨기기 - HideProgressPanel() /// <summary> /// 진행 패널 숨기기 /// </summary> private void HideProgressPanel() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(HideProgressPanel); this.dataGridView.Invoke(simpleDelegate); } else { this.progressPanel.Visible = false; this.progressPanel.SendToBack(); } } #endregion #region 진행 패널 리셋하기 - ResetProgressPanel(minimum, maximum) /// <summary> /// 진행 패널 리셋하기 /// </summary> /// <param name="minimum">최소값</param> /// <param name="maximum">최대값</param> private void ResetProgressPanel(int minimum, int maximum) { if(this.dataGridView.InvokeRequired) { ResetProgressPanelDelegate resetProgressPanelDelegate = new ResetProgressPanelDelegate(ResetProgressPanel); this.dataGridView.Invoke(resetProgressPanelDelegate, minimum, maximum); } else { this.progressLabel.Text = string.Empty; this.progressBar.Minimum = minimum; this.progressBar.Maximum = maximum; this.progressBar.Value = minimum; } } #endregion #region 진행 패널 설정하기 - SetProgressPanel(message, value) /// <summary> /// 진행 패널 설정하기 /// </summary> /// <param name="message">메시지</param> /// <param name="value">값</param> private void SetProgressPanel(string message, int value) { if(this.dataGridView.InvokeRequired) { SetProgressPanelDelegate setProgressPanelDelegate = new SetProgressPanelDelegate(SetProgressPanel); this.dataGridView.Invoke(setProgressPanelDelegate, message, value); } else { this.progressLabel.Text = message; this.progressBar.Value = value; this.progressLabel.Update(); this.progressBar.Update(); } } #endregion #region 데이터 지우기 - ClearData() /// <summary> /// 데이터 지우기 /// </summary> private void ClearData() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(ClearData); this.dataGridView.Invoke(simpleDelegate); } else { this.actualUseHeaderText = false; this.actualColumnCount = -1; this.sourceDataObject = null; this.sourceText = null; this.sourceTable = null; this.sourceReader = null; this.allowAutoColumnSizing = false; this.dataGridView.DataSource = null; } } #endregion #region 데이터 설정하기 - SetData() /// <summary> /// 데이터 설정하기 /// </summary> private void SetData() { if(!this.backgroundWorker.IsBusy) { this.okButton.Enabled = false; this.cancelButton.Enabled = false; this.backgroundWorker.RunWorkerAsync(); } } #endregion #region 데이터 그리드 데이터 소스 설정하기 - SetDataGridDataSource() /// <summary> /// 데이터 그리드 데이터 소스 설정하기 /// </summary> private void SetDataGridDataSource() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(SetDataGridDataSource); this.dataGridView.Invoke(simpleDelegate); } else { this.dataGridView.DataSource = this.sourceTable; for(int i = 0; i < this.dataGridView.Columns.Count; i++) { this.dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; this.dataGridView.Columns[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True; } } } #endregion #region 컬럼들 자동 크기 조정하기 - AutoResizeColumns() /// <summary> /// 컬럼들 자동 크기 조정하기 /// </summary> private void AutoResizeColumns() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(AutoResizeColumns); this.dataGridView.Invoke(simpleDelegate); } else { if(this.allowAutoColumnSizing) { for(int i = 0; i < this.dataGridView.Columns.Count; i++) { this.dataGridView.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.DisplayedCells); } } } } #endregion #region 행들 자동 크기 조정하기 - AutoResizeRows() /// <summary> /// 행들 자동 크기 조정하기 /// </summary> private void AutoResizeRows() { if(this.dataGridView.InvokeRequired) { SimpleDelegate simpleDelegate = new SimpleDelegate(AutoResizeRows); this.dataGridView.Invoke(simpleDelegate); } else { if(this.allowAutoRowSizing) { for(int i = 0; i < this.dataGridView.Rows.Count; i++) { this.dataGridView.AutoResizeRow(i); } } } } #endregion #region 선택된 행들 삭제 처리하기 - ProcessDeleteSelectedRows() /// <summary> /// 선택된 행들 삭제 처리하기 /// </summary> private void ProcessDeleteSelectedRows() { if(this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0) { return; } DataGridViewSelectedRowCollection collection = this.dataGridView.SelectedRows; for(int i = collection.Count; i >= 0; i--) { DataGridViewRow row = collection[i]; this.dataGridView.Rows.Remove(row); } } #endregion #region 더블 버퍼링 설정하기 - SetDoubleBuffered(control, doubleBuffered) /// <summary> /// 더블 버퍼링 설정하기 /// </summary> /// <param name="control">컨트롤</param> /// <param name="doubleBuffered">더블 버퍼링 여부</param> private void SetDoubleBuffered(Control control, bool doubleBuffered) { Type type = control.GetType(); PropertyInfo propertyInfo = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); propertyInfo.SetValue(control, doubleBuffered, null); } #endregion } } |