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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

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

        #region 생성자 - MainForm()

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

            Load                              += Form_Load;
            this.dataGridView.CellMouseEnter  += dataGridView_CellMouseEnter;
            this.dataGridView.CellDoubleClick += dataGridView_CellDoubleClick;
            this.dataGridView.CellMouseLeave  += dataGridView_CellMouseLeave;
        }

        #endregion

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

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable("Book");

            table.Columns.Add("Title"       , typeof(string));
            table.Columns.Add("Has Download", typeof(bool  ));
            table.Columns.Add("Year"        , typeof(int   ));
            table.Columns.Add("URL"         , typeof(string));
                        
            table.Rows.Add(new object[] { "Essential Algorithms"       , true , "2019", "http://www.csharphelper.com/algorithms2e.html" });
            table.Rows.Add(new object[] { "WPF 3d"                     , true , "2018", "http://www.csharphelper.com/wpf3d.html"        });
            table.Rows.Add(new object[] { "The C# Helper Top 100"      , true , "2017", "http://csharphelper.com/top100.htm"            });
            table.Rows.Add(new object[] { "Interview Puzzles Dissected", false, "2016", "http://www.csharphelper.com/puzzles.htm"       });

            this.dataGridView.DataSource = table;

            this.dataGridView.Columns["URL"].DefaultCellStyle.Font      = new Font(dataGridView.Font, FontStyle.Underline);
            this.dataGridView.Columns["URL"].DefaultCellStyle.ForeColor = Color.Blue;

            this.dataGridView.AutoResizeColumns();

            this.dataGridView.EditMode              = DataGridViewEditMode.EditProgrammatically;
            this.dataGridView.AllowUserToAddRows    = false;
            this.dataGridView.AllowUserToDeleteRows = false;
        }

        #endregion
        #region 데이터 그리드 뷰 셀 마우스 진입시 처리하기 - dataGridView_CellMouseEnter(sender, e)

        /// <summary>
        /// 데이터 그리드 뷰 셀 마우스 진입시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;

            int columnIndex = e.ColumnIndex;

            if(columnIndex == -1)
            {
                return;
            }

            if(dataGridView.Columns[columnIndex].Name == "Has Download")
            {
                dataGridView.Cursor = Cursors.Hand;
            }
            else if(dataGridView.Columns[columnIndex].Name == "URL")
            {
                dataGridView.Cursor = Cursors.UpArrow;
            }
        }

        #endregion
        #region 데이터 그리드 뷰 셀 더블 클릭시 처리하기 - dataGridView_CellDoubleClick(sender, e)

        /// <summary>
        /// 데이터 그리드 뷰 셀 더블 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;

            int columnIndex = e.ColumnIndex;

            if(columnIndex == -1)
            {
                return;
            }

            int rowIndex = e.RowIndex;

            if(dataGridView.Columns[columnIndex].Name == "Has Download")
            {
                bool value = (bool)dataGridView.Rows[rowIndex].Cells[columnIndex].Value;

                dataGridView.Rows[rowIndex].Cells[columnIndex].Value = !value;
            }
            else if(dataGridView.Columns[e.ColumnIndex].Name == "URL")
            {
                string url = (string)dataGridView.Rows[rowIndex].Cells[columnIndex].Value;

                Process.Start(url);
            }
        }

        #endregion
        #region 데이터 그리드 뷰 셀 마우스 진입시 처리하기 - dataGridView_CellMouseLeave(sender, e)

        /// <summary>
        /// 데이터 그리드 뷰 셀 마우스 진입시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dataGridView_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;

            if(dataGridView.Cursor != Cursors.Default)
            {
                dataGridView.Cursor = Cursors.Default;
            }
        }

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

댓글을 달아 주세요