How to bind a result of condition to a combobox?


Well-known member
Mar 3, 2014
Programming Experience
How can I bind a condition of Boolean data type?
there is a bit field which contain false or true in the Database, when scrolling the data Movenext or Moveprevious either of them must checked Yes Or No checkbox as the database field but I need to make a condition to use that in scrolling.

private void preparingScrollingData()
            using (SqlConnection con = new SqlConnection(ConnString1)) {

                string specifyDate = dateTimeInput1.Value.Date.ToString("yyyy/MM/dd");
                string strSqlCommand1 = "Select e.nRecordID as RecordID, e.sSeriNum as [Serial Nm], e.sSONum as [SO Number], e.sFormType as [Form Type]," + 
                    " e.sItemCode as [Item Code], e.bJudgement as Judgement, f.sDetectType as [Detect Type], e.dDate as [Record Date]," + 
                    " g.sPICName as [PIC Name], e.sRemark as [Remark]" + 
                    " FROM tbRecords e left outer join tbDetect f on f.nDetectTypeID = e.nDetectTypeID left outer join tbPIC g on g.nPICID = e.nPICID" + 
                    " WHERE e.dDate = '" + specifyDate + "' ORDER BY e.nRecordID ASC";
                using (SqlCommand command = new SqlCommand(strSqlCommand1, con)) {
                    SqlDataAdapter da = new SqlDataAdapter(command);

                    DataTable dt = new DataTable();

                    da.SelectCommand = command;

                    bs = new BindingSource();
                    bs.DataSource = dt;
                    txtSerialNum.DataBindings.Add(new Binding("Text", bs, "Serial Nm"));
                    txtSONum.DataBindings.Add(new Binding("Text", bs, "SO Number"));
                    cmbFormType.DataBindings.Add(new Binding("SelectedItem", bs, "Form Type"));
                    txtItemCode.DataBindings.Add(new Binding("Text", bs, "Item Code"));

                    chkYes.DataBindings.Add(new Binding("Checked", bs, "Judgement", true, DataSourceUpdateMode.OnPropertyChanged));

                    if (chkYes.Checked) {
                        chkYes.Checked = true;
                        chkNo.Checked = false;
                    } else {
                        chkYes.Checked = false;
                        chkNo.Checked = true;
                    cmbDetectType.DataBindings.Add(new Binding("SelectedItem", bs, "Detect Type"));
                    cmbPIC.DataBindings.Add(new Binding("SelectedItem", bs, "PIC Name"));
                    txtRemark.DataBindings.Add(new Binding("Text", bs, "Remark"));
From what you've said, it sounds like your UI makes no sense. You have one field that can be either True or False and you want to represent that with two CheckBoxes? No and double no! You represent a single Boolean value with a single CheckBox and that Boolean value is stored in the Checked property. The box displays a check mark if and only if Checked is True and, likewise, the box does not show a check mark if and only if Checked is False.

If you want to use two controls to represent that one value then you would use RadioButtons rather than CheckBoxes. CheckBoxes are independent whereas RadioButtons are designed to work as a group where only one in the group can be checked at a time. With CheckBoxes, you could have both Yes and No selected at the same time unless you wrote code to prevent it, whereas RadioButtons would automatically only allow one to be checked at a time.

If you do use RadioButtons though, you could not simply bind them both to the one DataColumn. If you wanted to use binding rather than code, you'd have two choices:

1. Add an extra column to your DataTable and set its Expression so it was always the opposite to your existing Boolean column. You could then bind your Yes RadioButton to the original column and your No RadioButton to the extra column.
2. Create a user control containing the two RadioButtons and expose a Boolean property in it that you can bind to. Internally, that property would update the RadioButtons as required.
You are right it's has quite good benefit to use RadioBoxes instead of CheckBox. But I have not fount a good sample in last a couple hours. Do you have a sample that works with Databate?
If I use one Radiobox then there would not be problem each time by scrolling the data. But as you know there two RadioBoxes as Yer and No so with that case I need to have a result of a condition that return which one of them must checked.
If I use one Radiobox then there would not be problem each time by scrolling the data. But as you know there two RadioBoxes as Yer and No so with that case I need to have a result of a condition that return which one of them must checked.

It's RadioButton and CheckBox. You would never have only one RadioButton because the whole point of RadioButtons is to allow only one of multiple options. As I have already said in an earlier post, if you're going to have two RadioButtons then you either need two columns to bind them to or wrap them in a user control that has a single property for binding that decides which RadioButton to check. Which of those two options do you want to implement?
Yes, I got used to type Radiobox instead of RadioButton since Visual C++ times. Sorry for the type.
I imagine below picture dizayn about groupbox which keeps two RadioButtons as one behave so How can I use groupbox's entity in binding then?
The GroupBox is irrelevant. I've given you the two options. Pick one.
Ok Understand those two items that you mentioned, I think It's better to follow the way of creating a User Control for this matter. The other one also looks nice method to do but User Control is more attractive for me now.
OK then. Add a new user control to your project and add two RadioButtons. You should check the Yes option by default. Add an appropriately-named property of type 'bool'. In the getter, return the value of the Yes RadioButton's Checked property. In the setter, check Yes if the value is 'true' and check No if it's 'false'. You should also add an event called SomePropertyChanged where 'SomeProperty' is the name of your property. You should handle the CheckedChanged event of the Yes RadioButton and, in the event handler, raise your own event. You can then bind to that property on your usder control just as you would any other property of any other control.

If you don't know how to define your own event, follow the Blog link in my signature below and check out my post on Custom Events.
Top Bottom