我有一个绑定到一个枚举组合框列一个DataGridView如下:检查DataGridViewComboBox是空
var D = (DataGridViewComboBoxColumn)dgvInputs.Columns[2];
D.ValueType = typeof(MyType);
D.ValueMember = "Value";
D.DisplayMember = "Display";
D.DataSource = new MyType[] {
MyType.Rev,
MyType.Model,
MyType.User,
MyType.Status
}.Select(x => new { Display = x.ToString(), Value = (int)x }).ToList();
在DataGridView然后绑定到数据表命名ParameterTable:
BindingSource ParamSource = new BindingSource();
ParamSource.DataSource = DataEntry.ParameterTable;
dgvInputs.AutoGenerateColumns = false;
dgvInputs.DataSource = ParamSource;
dgvInputs.Columns[0].DataPropertyName = "Name";
dgvInputs.Columns[1].DataPropertyName = "Prompt";
dgvInputs.Columns[2].DataPropertyName = "Type";
dgvInputs.Columns[3].DataPropertyName = "Width";
dgvInputs.Columns[4].DataPropertyName = "Default Value";
当用户完成编辑表格时,我需要验证它。特别是,我需要测试在每行中定义了类型,并且默认值与类型兼容。
问题是,我发现用于检查类型是否已经设置的每个测试都失败了。当我稍后尝试将值作为MyType转换为测试默认值的一部分时,出现错误。当我在调试器的空白类型单元格中检查.Value属性时,它显示值“{}”。
目前,我有用于测试的代码,在DataGridView自身的Validating事件中。我曾尝试过各种其他版本,他们也都失败了:
foreach (DataGridViewRow Row in dgvInputs.Rows) {
if (!Row.IsNewRow) {
// test other columns ...
DataGridViewComboBoxCell Cell = (DataGridViewComboBoxCell)(Row.Cells[2]);
if (Cell == null || Cell.Value as string == string.Empty) {
// Error ...
}
MyType PType = (MyType)(Cell.Value);
如何测试是否DataGridViewComboBox细胞尚未确定,而这是什么价值“{}”?
仅供参考 - 我使用VS 2008和.Net 3.5 SP1。不是我的选择。正是我所能得到的。
什么是'DataEntry.ParameterTable'的类型? –
对不起。这是一个数据表 –
if(Cell.Value == null)应该告诉什么时候没有设置单元 –