C# dataGridView限制某一行的单元格只输入数字的方法之一

Part1. dataGridView限制某一行的单元格只输入数字的方法之一

首先,参考了如下的文章:

点击打开链接

然后,给你的dataGridView1添加dataGridView1_EditingControlShowing事件,并在事件的上方定义如下变量:

    public DataGridViewTextBoxEditingControl CellEdit = null;

       // dataGridView1_EditingControlShowing事件的完整代码如下,我是限制第一行和第二行的单元格里只能输入数字:

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                //如果是第1行和第二行,就限制只能输入数字,第三行不受限制
                if ((dataGridView1.CurrentCell.RowIndex == 0) || (dataGridView1.CurrentCell.RowIndex == 1))
                {
                    if (this.dataGridView1.CurrentCellAddress.X == i)
                    {
                        {
                            if (this.dataGridView1.CurrentCellAddress.X == i)
                            {
                                CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                                CellEdit.SelectAll();
                                CellEdit.KeyPress += Cells_KeyPress; //绑定事件
                            }
                        }
                    }
                }
                else //不然,移除这个事件。。。
                {
                    CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                    CellEdit.SelectAll();
                    CellEdit.KeyPress -= Cells_KeyPress; //绑定事件
                }
            }
        }

       private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
        {
            if (!(char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
                e.Handled = true;
            //if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
            //if (e.KeyChar == '\b') e.Handled = false;
        }

特别注意的是,如果不把事件订阅移除,则每个单元格它都检查,不管是不是你选择的行。。。

Part2.dataGridView的其它使用总结

            //不允许用户添加行
            dataGridView1.AllowUserToAddRows = false;

            //不显示列标题。。则dataGridView1最左边的那一列不会出现
            dataGridView1.ColumnHeadersVisible = false;

            //更改列标题颜色。。
            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGray;

            //清空列表
            dataGridView1.Rows.Clear();

            //如果允许用户动态添加行,则以下语句意思是再添加28行,所以一共是29行。。。 
            //否则的话,则是总共添加28行        
            dataGridView1.Rows.Add(28);

            //把dataGridView1高度设为均等,
            int h = dataGridView1.Height;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Height = Convert.ToInt16(h / 32);
            }
            //如果发现还是跟页面不符,可以在高度后面修改数值,像这样:
            dataGridView1.Rows[i].Height = Convert.ToInt16(h / 31) + 3;

            //第i行第j列元素的访问方式..下标都是从0开始
            dataGridView1.Rows[i].Cells[j].Value = "8:30-9:30";

            //修改第12行的第0列、1列、2列的背景色为深灰色。。。
            dataGridView1[0, 12].Style.BackColor = Color.DarkGray;
            dataGridView1[1, 12].Style.BackColor = Color.DarkGray;
            dataGridView1[2, 12].Style.BackColor = Color.DarkGray;

            //设置dataGridView1的行字体大小
            dataGridView1.RowsDefaultCellStyle.Font = new Font("宋体", 12, FontStyle.Regular);

            //设置dataGridView1的列宽度根据单元格的内容自适应
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值