C#空间

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

namespace WindowsFormsApplication
{
    public class CustomDataGridView : DataGridView
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                return this.ProcessRightKey(keyData);
            }
            return base.ProcessDialogKey(keyData);
        }

        public new bool ProcessRightKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                //第一种情况:只有一行,且当光标移到最后一列时,新增一行
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.RowCount ==1))
                {
                    //新增一行
                    base.Rows.Add();
                    base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
                    return true;
                }
                //第二种情况:有多行,且当光标移到最后一列时,移到下一行第一个单元,
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) &&(base.CurrentCell.RowIndex < (base.RowCount - 1)))
                {
                   // base.Rows.Add();
                    base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
                    return true;
                }
                //第三种情况:有多行,且当光标移到最后一行最后一列时,移到下一行第一个单元,新增一行
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex== base.RowCount -1))
                {
                    //新增一行
                    base.Rows.Add();
                    base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
                    return true;
                }
                return base.ProcessRightKey(keyData);
            }
            return base.ProcessRightKey(keyData);
        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                return this.ProcessRightKey(e.KeyData);
            }
            //if (e.KeyCode == Keys.F4)
            //{
            //    return this.ProcessRightKey(e.KeyData);
            //}
            return base.ProcessDataGridViewKey(e);
        }


    }

}

posted on 2010-07-17 22:58  小海洋  阅读(638)  评论(0编辑  收藏  举报