Jay*_*ggs 23
使用的MaxInputLength属性DataGridViewTextBoxColumn.
此属性可通过Designer或代码获得:
((DataGridViewTextBoxColumn)dataGridView1.Columns[yourColumn]).MaxInputLength = 6;
Run Code Online (Sandbox Code Playgroud)
请使用DataGridView的CellValueChanged事件.
在事件的处理程序中,您可以检查DataGridViewCellEventArgs参数的ColumnIndex和RowIndex属性,以识别编辑网格的感兴趣字段,然后 - 采取适当的操作.
如其他答案中所述 - 限制DataGridView字段的文本长度的最自然方式是修改相应的网格列属性.可以在" 编辑列"表单上更改网格列的属性,该表单是在窗体设计器中为网格控件调用的,右键菜单项为" 编辑列...":
