Enter實現Datagridview逐單元格編輯

新建一個用戶控件,把代碼複製進去,生成,就可以做成dll文件,後面要使用直接在項目裏添加引用

代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserControl
{
    public partial class CCDataGridView : DataGridView
    {

        bool _isFill=true;
        bool _isEditOnEnter = true;


        public CCDataGridView()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 判斷是否把DG中的最後一行FILL整個窗體。默認值爲True;
        /// </summary>
        [Description("判斷是否把DataGridView中的最後一列Fill整個窗體。默認值爲True")]
        public bool IsFillForm
        {
            get
            {
                return _isFill;
            }
            set
            {
                _isFill = value;
            }
        }


        /// <summary>
        /// 是否將DataGridView的編輯模式設爲EditOnEnter。默認值爲True
        /// </summary>
        [Description("是否將DataGridView的編輯模式設爲EditOnEnter。默認值爲True")]
        public bool IsEditOnEnter
        {
            get
            {
                return _isEditOnEnter;
            }
            set
            {
                _isEditOnEnter = value;
                if (_isEditOnEnter)
                {
                    this.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
                }
                else
                {
                    this.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnKeystrokeOrF2;
                }
            }
        }


        protected override void OnPaint(PaintEventArgs pe)
        {
            if (IsFillForm)
            {
                //調整最後一列的寬度使其佔據DataGridView的剩餘部分




                //for (int i = this.ColumnCount - 1; i >= 0; i--)
                //{
                //    if (this.Columns[i].Visible)
                //    {


                //        this.Columns[i].MinimumWidth = 5;


                //        this.Columns[i].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.NotSet;
                //        break;
                //    }
                //}
            }
            // 調用基類 OnPaint
            base.OnPaint(pe);
        }
        
        //顯示行號
        private void CCDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {                          
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                 e.RowBounds.Location.Y,
                 this.RowHeadersWidth - 4,
                 e.RowBounds.Height);


            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                this.RowHeadersDefaultCellStyle.Font,
                rectangle,
                this.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);            
        }


        //更改DataGridView下拉框控件樣式
        private void CCDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                ComboBox cb = (ComboBox)e.Control;
                if (cb != null)
                {
                    cb.DropDownStyle = ComboBoxStyle.DropDown;
                }
            }
        }


        private void CCDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewTextBoxColumn))
            {
                if (this[e.ColumnIndex, e.RowIndex].Value == null) return;
                this[e.ColumnIndex, e.RowIndex].Value = this[e.ColumnIndex, e.RowIndex].Value.ToString().Trim();
            }
        }


       // [System.Security.Permissions.UIPermission(
       //System.Security.Permissions.SecurityAction.LinkDemand,
       //Window = System.Security.Permissions.UIPermissionWindow.AllWindows)]
       // protected override bool ProcessDialogKey(Keys keyData)
       // {
       //     if ((keyData & Keys.KeyCode)== Keys.Enter)//回車
       //     {
       //         if (this.ReadOnly == false)//DataGridView可編輯
       //         {
       //             if (this.CurrentCell.ColumnIndex < this.ColumnCount - 1)//當前單元格列索引小於最大列數
       //             {
       //                 int nextCellIndex = -1;
       //                 for (int i = this.CurrentCell.ColumnIndex + 1; i < this.ColumnCount; i++)//從當前單元格所在列循環
       //                 {
       //                     if (this.CurrentRow.Cells[i].Visible == true && this.CurrentRow.Cells[i].ReadOnly == false)//當前單元格可編輯並且顯示的單元格
       //                     {
       //                         nextCellIndex = i;//當前單元格後含有可編輯的單元格
       //                         break;
       //                     }
       //                 }


       //                 if (nextCellIndex == -1)//當前單元格後沒有可編輯的單元格
       //                 {
       //                     if (this.CurrentRow.Index < this.RowCount - 1)//當前行索引不於最大行號
       //                     {
       //                         for (int i = 0; i < this.ColumnCount; i++)//從第一列循環
       //                         {
       //                             if (this.Rows[this.CurrentRow.Index + 1].Cells[i].Visible == true && this.Rows[this.CurrentRow.Index + 1].Cells[i].ReadOnly == false)//當前行下行中可編輯並且顯示的單元格
       //                             {
       //                                 try
       //                                 {
       //                                     this.CurrentCell = this.Rows[this.CurrentRow.Index + 1].Cells[i];//設置下一行中第一個可編輯的單元格爲當前單元格
       //                                 }
       //                                 catch
       //                                 {
       //                                 }
       //                                 return true;
       //                             }
       //                         }
       //                     }
       //                 }
       //                 else//設置當前單元格後的第一個可編輯的單元格爲當前單元格
       //                 {
       //                     try
       //                     {
       //                         this.CurrentCell = this.CurrentRow.Cells[nextCellIndex];
       //                     }
       //                     catch
       //                     {
       //                     }
       //                     return true;
       //                 }
       //             }
       //             else//當前列爲最後一列
       //             {
       //                 if (this.CurrentRow.Index < this.RowCount - 1)//當前行索引不於最大行號
       //                 {
       //                     for (int i = 0; i < this.ColumnCount; i++)//從第一列開始循環
       //                     {
       //                         if (this.Rows[this.CurrentRow.Index + 1].Cells[i].Visible == true && this.Rows[this.CurrentRow.Index + 1].Cells[i].ReadOnly == false)
       //                         {
       //                             try
       //                             {
       //                                 this.CurrentCell = this.Rows[this.CurrentRow.Index + 1].Cells[i];//設置下一行中第一個可編輯的單元格爲當前單元格
       //                             }
       //                             catch
       //                             {
       //                             }
       //                             return true;
       //                         }
       //                     }
       //                 }
       //             }
       //         }
       //     }          


       //     return base.ProcessDialogKey(keyData);
       // }


        [System.Security.Permissions.SecurityPermission(
            System.Security.Permissions.SecurityAction.LinkDemand, Flags =
            System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//回車
            {
                if (this.ReadOnly == false)//DataGridView可編輯
                {
                    if (this.CurrentCell.ColumnIndex < this.ColumnCount - 1)//當前單元格列索引小於最大列數
                    {
                        int nextCellIndex = -1;
                        for (int i = this.CurrentCell.ColumnIndex + 1; i < this.ColumnCount; i++)//從當前單元格所在列循環
                        {
                            if (this.CurrentRow.Cells[i].Visible == true && this.CurrentRow.Cells[i].ReadOnly == false)//當前單元格可編輯並且顯示的單元格
                            {
                                nextCellIndex = i;//當前單元格後含有可編輯的單元格
                                break;
                            }
                        }


                        if (nextCellIndex == -1)//當前單元格後沒有可編輯的單元格
                        {
                            if (this.CurrentRow.Index < this.RowCount - 1)//當前行索引不於最大行號
                            {
                                for (int i = 0; i < this.ColumnCount; i++)//從第一列循環
                                {
                                    if (this.Rows[this.CurrentRow.Index + 1].Cells[i].Visible == true && this.Rows[this.CurrentRow.Index + 1].Cells[i].ReadOnly == false)//當前行下行中可編輯並且顯示的單元格
                                    {
                                        try
                                        {
                                            this.CurrentCell = this.Rows[this.CurrentRow.Index + 1].Cells[i];//設置下一行中第一個可編輯的單元格爲當前單元格
                                        }
                                        catch
                                        {
                                        }
                                        return true;
                                    }
                                }
                            }
                        }
                        else//設置當前單元格後的第一個可編輯的單元格爲當前單元格
                        {
                            try
                            {
                                this.CurrentCell = this.CurrentRow.Cells[nextCellIndex];
                            }
                            catch
                            {
                            }
                            return true;
                        }
                    }
                    else//當前列爲最後一列
                    {
                        if (this.CurrentRow.Index < this.RowCount - 1)//當前行索引不於最大行號
                        {
                            for (int i = 0; i < this.ColumnCount; i++)//從第一列開始循環
                            {
                                if (this.Rows[this.CurrentRow.Index + 1].Cells[i].Visible == true && this.Rows[this.CurrentRow.Index + 1].Cells[i].ReadOnly == false)
                                {
                                    try
                                    {
                                        this.CurrentCell = this.Rows[this.CurrentRow.Index + 1].Cells[i];//設置下一行中第一個可編輯的單元格爲當前單元格
                                    }
                                    catch
                                    {
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                }
            }


            return base.ProcessDataGridViewKey(e);
        }        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章