我有一个 DataGridView
控件,我想在其中限制用户只能为特定列下的单元格输入数值。如何在 DataGridView
单元格中完成这种类型的验证?
当我创建一个简单的文本框时这是可能的,但如何验证 DataGridView
单元格?
最佳答案
您可以设置列将保存的数据类型,如以下代码片段所示:
var columnSpec = new DataColumn();
columnSpec.DataType = <your type>
// Other initialisation
dataTable.Columns.Add(columnSpec);
dataGridView.DataSource = dataTable;
如果您直接使用 DataGridView
,则 DataGridViewColumn
类具有以下属性:
ValueType - Gets or sets the data type of the values in the column's cells.
如果您使用此类而不是更专业的类 DataGridViewCheckBoxColumn
等创建列,这可能确实是您想要的。
关于datagridview - 如何限制用户在 DataGridView 列的特定单元格中仅输入数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/963869/