3

I am databinding a checkbox to a bindingsource, if i dont click the checkbox it will return a null value to my database.

This is the databinding code:

 checkGehuwd.DataBindings.Add("Checked", PersonenBindingSource, "gehuwd", true,
            DataSourceUpdateMode.OnPropertyChanged);

how do i return a default value of false?

Greetings Andy

9
  • Why not setting default value false at db? Commented Sep 30, 2016 at 20:53
  • i tried that it is still setting it as null. Commented Sep 30, 2016 at 20:59
  • 1
    is gehund a nullable bool? Commented Sep 30, 2016 at 20:59
  • its a integer value in a sqlite database, linked to a dataset, linked to a bindingsource, databound to a checkbox. Commented Sep 30, 2016 at 21:01
  • 1
    I don't think binding non-boolean to check box is preferred either but whatever works. Commented Sep 30, 2016 at 21:20

2 Answers 2

5

You can use data-binding to CheckState property this way:

checkBox1.DataBindings.Add("CheckState", bs, "DataFieldName", true,
    DataSourceUpdateMode.OnPropertyChanged, CheckState.Indeterminate);

This way, when the value of filed in the data source is null, CheckState.Indeterminate will be shown in UI.

Note

  • If you want the default value of column be 0 or unchecked, then set the DefaultValue of DataColumn to 0.

  • If you want to let the user also set value of the field to null, it's enough to set ThreeState property of CheckBox to true.

Example

dt = new DataTable();
var c1 = dt.Columns.Add("C1", typeof(int));
c1.AllowDBNull = true;

//Uncomment the next statement if you want default value be 0 = unchecked
//c1.DefaultValue = 0;

//Uncomment the next statement if you want to allow the user to set value to null
//checkBox1.ThreeState = true;

var bs = new BindingSource();
bs.DataSource = dt;
checkBox1.DataBindings.Add("CheckState", bs, "C1", true,
    DataSourceUpdateMode.OnPropertyChanged, CheckState.Indeterminate);
this.bindingNavigator1.BindingSource = bs;
Sign up to request clarification or add additional context in comments.

5 Comments

This will still insert nulls when i do not click it, even when setting CheckState.true as default
It will insert 1 when you click on check mark and make the checkbox show check mark. Inserts 0 when you click and uncheck. Inserts null when you click and set the checkbox to indeterminate which shows a square in checkbox.
To test it, simply use the code which I provided in example. Such code could be simply tested without need to any database.
Make sure you read the Note part and comments in example.
It seems the post answers your question, let me know if you have any question about the answer or if you find it useful :)
0

Just to vent my frustration - simple things like these make me hate all this Microsoft databinding. Why can't life just be easy by default, like checkbox being true of false, 1 or 0. If someone wants to complicate things with implementing tristates etc. that should be the extra option, not the default option.

Even thou Tristate property is set to False, the default value of the checkbox seems to be NULL anyway. And the default value in my SQL database is set to (0) for that field. So I ended up with a dirty solution like this:

// before saving to db
if (!checkbox.Checked) {
    checkbox.Checked = true;
    checkbox.Checked = false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.