设置DataGridView列中的字符输入最大值 (今天有收获,学习中)

        http://slcfhr.cnblogs.com/

        经常性的问题.我们在DataGridView单元格中输入的数据往往因为字节

不和数据帮订或是DataSet表中的数据不符,我们常用的方法是异常处理..    但是,

我们直接把DataGridView中的列的字节做一个控制,不是少很多异常吗?

        控制列中单元格输入最大字符数可以达到这个目的。

我用的代码是这样的:

            DataGridViewTextBoxColumn Column1 = new DataGridViewTextBoxColumn();
            DataGridViewCellStyle dataGridViewCellStyle1 
= new DataGridViewCellStyle();
            
this.Column1.DataPropertyName = "单位";
            dataGridViewCellStyle1.NullValue 
= null;
            
this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
            
this.Column1.HeaderText = "单位"

            
//数据库中这个字段为6个字节
            this.Column1.MaxInputLength = 6;
            
this.Column1.Name = "单位"

            
//添加到DataGridView
            this.dataGridView1.Columns.AddRange(DataGridViewColumn[] {
            
this.Column1}
);

这样就只能在"单位"列中输入6个字符了。。大大减少了异常

:)http://slcfhr.cnblogs.com/
posted @ 2006-07-21 02:08  邵利超  阅读(3274)  评论(0编辑  收藏  举报