datagridview - 如何限制用户在 DataGridView 列的特定单元格中仅输入数值?

标签 datagridview

我有一个 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/

相关文章:

c# - 按 T​​AB 键时绕过 DataGridView 中的只读单元格

c# - 在 C# 中只清除 1 行 DataGridView

c# - 从 DGV 中删除——索引 [x] 没有值

c# - 将 DataGridViewAutoFilter 与 Equin BindingListView 结合使用

datagridview - 如何在datagridview的同一个单元格中添加复选框和标签?

c# - 在 DataGridView 中对日期进行排序

c# - 我可以有一个 DataGridView 单元格,我可以在其中键入内容或从下拉列表中进行选择吗?

c# - 在 db - C# 中存储静态数据好吗?

c# - DataGridView更改单元格背景并恢复默认样式

c# - DataGridView 限制用户输入