class DemandsDataGridView:DataGridView
    {
        DataGridViewCell previousCell;
        bool resetting = false;

        public DemandsDataGridView()
        {
            this.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
            this.CellLeave += new DataGridViewCellEventHandler(dataGridView_CellLeave);
            this.SelectionChanged += new EventHandler(dataGridView_SelectionChanged);
        }

        void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (this.CurrentCell != null && this.resetting==false)
            {
                if (this.CurrentCell.ColumnIndex == 0 || this.CurrentCell.RowIndex == 0)
                {
                    this.resetting = true;
                    this.CurrentCell.Selected = false;
                    this.CurrentCell = this.previousCell;
                    this.resetting = false;
                }
            }
        }

        void dataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.previousCell = this.CurrentCell;
        }
    }


 

선택이 변경 되었을 때

선택을 막고 싶은 영역으로 들어온다면

Seleted 속성을 false로 변경하고

이전 선택된 Cell로 선택된 Cell을 변경한다.

+ Recent posts