Search code examples
c#winformsdatagridviewdatagridcomboboxcolumn

Check if DataGridViewComboBox is empty


I have a datagridview with a combobox column that is bound to an enum as follows:

     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();

The datagridview is then bound to a DataTable named 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";

When the user finishes editing the table, I need to validate it. In particular, I need to test that the Type has been defined in each row, and that the Default Value is compatible with the Type.

The problem is, every test I've found for checking if the Type has been set has failed. When I later try to cast the value as MyType as part of testing the default value, I get an error. When I check the .Value property on the empty Type cell in the debugger, it shows a value of "{}".

Currently, I have this code for the test, in the Validating event for the datagridview itself. I have tried various other versions and they have also failed:

     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);

How can I test if a DataGridViewComboBox cell has not been set, and what is this value "{}"?

FYI - I am using VS 2008, and .Net 3.5 SP1. Not my choice. Just what is available to me.


Solution

  • There are a couple problems with this code.

    First, D.ValueType = typeof(MyType); is incorrect because from what I see, you are binding to int field. Just remove that line, ValueType will be inferred from the data source.

    Now, the main issue. When binding to a data table, the non entered value is represented by DBNull.Value. I would suggest you checking for both null and DBNull.Value. When entered, the value type in your case will be int, but you can safely unbox it to MyType.

    The code should be something like this

    //...
    var value = Row.Cells[2].Value;
    if (value == null || value == DBNull.Value)
    { 
        // Error ...
    }
    else
    {
        var type = (MyType)value;
        // Check if type contains a valid value ...
    }