첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 마우스 DOWN 행 인덱스
        /// </summary>
        private int mouseDownRowIndex;

        /// <summary>
        /// 마우스 DOWN 사각형
        /// </summary>
        private Rectangle mouseDownRectangle;

        /// <summary>
        /// 드레그 DROP 행 인덱스
        /// </summary>
        private int dragDropRowIndex;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 리스트를 설정한다.

            List<DataItem> list = new List<DataItem>();

            list.Add(new DataItem { Item = "형식"          , Description = "쌍발 복좌 고등 훈련기"                                     });
            list.Add(new DataItem { Item = "전폭"          , Description = "7.70m"                                                     });
            list.Add(new DataItem { Item = "전장"          , Description = "14.14m"                                                    });
            list.Add(new DataItem { Item = "전고"          , Description = "3.92m"                                                     });
            list.Add(new DataItem { Item = "주익 면적"     , Description = "15.8m²"                                                   });
            list.Add(new DataItem { Item = "최대 이륙 중량", Description = "5,466kg"                                                   });
            list.Add(new DataItem { Item = "엔진"          , Description = "GE J85-GE-5R 터보 제트 엔진 (Dry 995kg, A/B 1,497kg) × 2" });
            list.Add(new DataItem { Item = "최대 속도"     , Description = "M 1.23/10,973m"                                            });
            list.Add(new DataItem { Item = "순항 속도"     , Description = "M 0.95/10,973m"                                            });
            list.Add(new DataItem { Item = "상승 한도"     , Description = "16,335m"                                                   });
            list.Add(new DataItem { Item = "항속 거리"     , Description = "1,768km(최대 연료)"                                        });
            list.Add(new DataItem { Item = "이륙활주거리"  , Description = "762m"                                                      });
            list.Add(new DataItem { Item = "착륙활주거리"  , Description = "915m"                                                      });
            list.Add(new DataItem { Item = "승무원"        , Description = "2명"                                                       });
            list.Add(new DataItem { Item = "가격"          , Description = "756,000달러(1961년)"                                       });

            #endregion
            #region 데이터 그리드 뷰를 설정한다.

            this.dataGridView.AllowDrop = true;

            #endregion

            SetDataGridViewData(list);

            #region 이벤트를 설정한다.

            this.dataGridView.RowPostPaint += dataGridView_RowPostPaint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 데이터 그리드 뷰 행 포스트 PAINT 처리하기 - dataGridView_RowPostPaint(sender, e)

        /// <summary>
        /// 데이터 그리드 뷰 행 포스트 PAINT 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;

            string rowNumber = (e.RowIndex + 1).ToString();

            StringFormat stringFormat = new StringFormat() 
            { 
                Alignment     = StringAlignment.Center, 
                LineAlignment = StringAlignment.Center
            };

            Rectangle drawRectangle = new Rectangle
            (
                e.RowBounds.Left,
                e.RowBounds.Top,
                dataGridView.RowHeadersWidth,
                e.RowBounds.Height
            );

            e.Graphics.DrawString(rowNumber, this.Font, SystemBrushes.ControlText, drawRectangle, stringFormat);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Functioon

        #region 데이터 그리드 뷰 셀 스타일 구하기 - GetDataGridViewCellStyle(backgroundColor, alignment)
 
        /// <summary>
        /// 데이터 그리드 뷰 셀 스타일 구하기
        /// </summary>
        /// <param name="backgroundColor">배경색</param>
        /// <param name="alignment">정렬</param>
        /// <returns>데이터 그리드 뷰 셀 스타일</returns>
        private DataGridViewCellStyle GetDataGridViewCellStyle(Color backgroundColor, DataGridViewContentAlignment alignment)
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
 
            style.Alignment = alignment;
            style.BackColor = backgroundColor;
 
            return style;
        }
 
        #endregion
        #region 데이터 그리드 뷰 데이터 설정하기 - SetDataGridViewData(dataSource)
 
        /// <summary>
        /// 데이터 그리드 뷰 데이터 설정하기
        /// </summary>
        /// <param name="dataSource">데이터 소스</param>
        private void SetDataGridViewData(object dataSource)
        {
            if(this.dataGridView.DataSource != null)
            {
                IDisposable disposable = this.dataGridView.DataSource as IDisposable;
 
                this.dataGridView.DataSource = null;
 
                if(disposable != null)
                {
                    disposable.Dispose();
 
                    disposable = null;
                }
            }
 
            #region 리스트 데이터 그리드 뷰 컬럼을 설정한다.
 
            this.dataGridView.Columns.Clear();
 
            DataGridViewColumn column;
 
            column = new DataGridViewTextBoxColumn();
 
            column.Width                      = 150;
            column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            column.HeaderText                 = "항목";
            column.SortMode                   = DataGridViewColumnSortMode.NotSortable;
            column.DataPropertyName           = "Item";
            column.ValueType                  = typeof(string);
            column.DefaultCellStyle           = GetDataGridViewCellStyle(Color.White, DataGridViewContentAlignment.MiddleLeft);
 
            this.dataGridView.Columns.Add(column);
 
            column = new DataGridViewTextBoxColumn();
 
            column.Width                      = 500;
            column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            column.HeaderText                 = "설명";
            column.SortMode                   = DataGridViewColumnSortMode.NotSortable;
            column.DataPropertyName           = "Description";
            column.ValueType                  = typeof(string);
            column.DefaultCellStyle           = GetDataGridViewCellStyle(Color.White, DataGridViewContentAlignment.MiddleLeft);
 
            this.dataGridView.Columns.Add(column);

            #endregion

            this.dataGridView.DataSource = dataSource;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요