博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView焦点控制
阅读量:4145 次
发布时间:2019-05-25

本文共 2902 字,大约阅读时间需要 9 分钟。

WinForm自带的DataGridView不能对某些特定单元格进行能否获取焦点的控制的。通过重写DataGridViewDataGridView.OnKeyDown, DataGridView.ProcessDataGridViewKey, DataGridView.SetCurrentCellAddressCore, DataGridView.SetSelectedCellCore, DataGridView.OnMouseDown的方法可以实现对焦点的控制逻辑。

 

下面的示例通过通过重写DataGridViewSetCurrentCellAddressCoreSetSelectedCellCore的方法,实现控制DataGridView的某一列不能获取焦点。

 

部分代码:

public partial class RestrictFocusForm1 : Form    {        public RestrictFocusForm1()        {            InitializeComponent();        }        private void RestrictFocusForm1_Load(object sender, EventArgs e)        {            DataTable dt = new DataTable();            dt.Columns.Add("c1");            dt.Columns.Add("c2");            dt.Columns.Add("c3");            for (int j = 0; j < 10; j++)            {                dt.Rows.Add("aaa" + j.ToString(), "bbb");            }            this.myDataGridView1.DataSource = dt;            this.myDataGridView1.ColumnToSkip = 1;        }    }    public class myDataGridView : DataGridView    {        private int columnToSkip = -1;        public int ColumnToSkip        {            get { return columnToSkip; }            set { columnToSkip = value; }        }        protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex,            bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)        {            if (columnIndex == this.columnToSkip && this.columnToSkip != -1)            {                if (this.columnToSkip == this.ColumnCount - 1)                {                    return base.SetCurrentCellAddressCore(0, rowIndex + 1,                        setAnchorCellAddress, validateCurrentCell, throughMouseClick);                }                else                {                    if (this.ColumnCount != 0)                    {                        return base.SetCurrentCellAddressCore(columnIndex + 1, rowIndex,                              setAnchorCellAddress, validateCurrentCell, throughMouseClick);                    }                }            }            return base.SetCurrentCellAddressCore(columnIndex, rowIndex,               setAnchorCellAddress, validateCurrentCell, throughMouseClick);        }        protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)        {            if (columnIndex == this.columnToSkip)            {                if (this.columnToSkip == this.ColumnCount - 1)                {                    base.SetSelectedCellCore(0, rowIndex + 1, selected);                }                else                {                    if (this.ColumnCount != 0)                    {                        base.SetSelectedCellCore(columnIndex + 1, rowIndex, selected);                    }                }            }            else            {                base.SetSelectedCellCore(columnIndex, rowIndex, selected);            }        }    }

 

 

 

转载请注明出处!!!

你可能感兴趣的文章
1060 Are They Equal (25 分)
查看>>
83. Remove Duplicates from Sorted List(easy)
查看>>
88. Merge Sorted Array(easy)
查看>>
leetcode刷题191 位1的个数 Number of 1 Bits(简单) Python Java
查看>>
leetcode刷题198 打家劫舍 House Robber(简单) Python Java
查看>>
NG深度学习第一门课作业2 通过一个隐藏层的神经网络来做平面数据的分类
查看>>
leetcode刷题234 回文链表 Palindrome Linked List(简单) Python Java
查看>>
NG深度学习第二门课作业1-1 深度学习的实践
查看>>
Ubuntu下安装Qt
查看>>
Qt札记
查看>>
我的vimrc和gvimrc配置
查看>>
hdu 4280
查看>>
禁止使用类的copy构造函数和赋值操作符
查看>>
C++学习路线
查看>>
私有构造函数
查看>>
组队总结
查看>>
TitledBorder 设置JPanel边框
查看>>
DBCP——开源组件 的使用
查看>>
抓包工具
查看>>
海量数据相似度计算之simhash和海明距离
查看>>