Question

This fails:

dtaAutomaticFlag.DataBindings.Add("Checked", BndSource, "AutomaticFlag");

most probably because AutomaticFlag is defined as:

public SqlByte AutomaticFlag { get; set; }

SQL Server is tinyint. The reason is that i dont only need 0 and 0 as values.

I need to be checked if the value is 1 , unchecked for ALL other values.

One thing that i thought is to create an extra property AutomaticFlagBoolean in my class and check the AutomaticFlag and bind to the new property.

This is what i tried but i get always false:

    private SqlByte _automaticFlag;
    public SqlByte AutomaticFlag
    {
        get { return _automaticFlag; }
        set
        {
            if (_automaticFlagBoolean)
            {
                _automaticFlag = 1;
            }
            else
            {
                _automaticFlag = 0;
            }
        }
    }

    private bool _automaticFlagBoolean;
    public bool AutomaticFlagBoolean
    {
        get
        {
            if (_automaticFlag == 1)
            {
                return _automaticFlagBoolean = true;
            }
            else
            {
                return _automaticFlagBoolean = false;
            }
        }
        set { _automaticFlagBoolean = value; }
    }

this is in my binding:

dtaAutomaticFlag.DataBindings.Add("Checked", BndSource, "AutomaticFlagBoolean");
Was it helpful?

Solution

The checkbox control expects a boolean value for binding.

Try using a local variable to hold the value:

private SqlByte _automaticFlag;
private bool _automaticFlagBoolean;

public SqlByte AutomaticFlag
{
    get { return _automaticFlag; }
    set
    {
        _automaticFlag = value;
        _automaticFlagBoolean = (_automaticFlag != 0);
    }
}

public bool AutomaticFlagBoolean
{
    get
    {
        return _AutomaticFlagBookean;
    }
    set { _automaticFlagBoolean = value; 
       if (_automaticFlagBoolean) {
          _automaticFlag = 1;
       } else {
          _automaticFlag = 0;
       }
    }
}

Then bind as so:

dtaAutomaticFlag.DataBindings.Add("Checked", BndSource, "AutomaticFlagBoolean");

Your class should also implement INotifyPropertyChanged as well but that's a little beyond the scope of your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top