This is a migrated thread and some comments may be shown as answers.

RadCheckBox DataBindings problem

1 Answer 218 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Jasper
Top achievements
Rank 1
Jasper asked on 10 Apr 2014, 12:55 PM
Hi there,

Another databindings question concerning RadCheckBox. I've seen a few floating around, but for some reason I just cannot get it to work.

First, I create a bindingsource, a button and a checkbox in designer. On load I do the following in codebehind:

private MyEntities dbContext = new MyEntities();
 
private void ManageClientsForm_Load(object sender, EventArgs e)
{
    radCheckBox1.DataBindings.Add("IsChecked", bindingSource1, "active");
 
    Client c = (from c in dbContext.Clients
                where c.Id = 1000
                select c).First();
 
    bindingSource1.DataSource = c;
}
 
private void radButton1_Click(object sender, EventArgs e)
{
    if (bindingSource1.DataSource == null) { return; }
 
    Client c = (Client)bindingSource1.DataSource;
 
    bool hasChanges = (from c in dbContext.ChangeTracker.Entries<Clients>()
                       where c.Entity == c & c.State != System.Data.Entity.EntityState.Unchanged
                       select c).Any();
 
    if (hasChanges)
    {
        dbContext.SaveChanges();
    }
}

hasChanges returns always false. The other textboxes I have bound in similar fashion have no problems.

I use EF6 and the field 'active' is a bit column (which is seen as bool due to EF, therefore no parse/format). I also changed the threestate-property of the checkbox to false.

Any ideas as to how I can fix this?

Thanks in advance,

Jasper Gielen

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 15 Apr 2014, 01:16 PM
Hi Jasper,

Thank you for contacting us.

Due to the specifics of the simple databinding in Windows Forms which was in conflict with our current implementation, we had to add a new property which can be used in this scenario. You will need to bind to the CheckState since it has the appropriate corresponding event CheckStateChanged. You will also need to use the Parse/Format events:
Binding binding = new Binding("CheckState", bindingSource1, "Active", true, DataSourceUpdateMode.OnPropertyChanged);
binding.Format += binding_Format;
binding.Parse += binding_Parse;
checkBox.DataBindings.Add(binding);
 
void binding_Format(object sender, ConvertEventArgs e)
{
    e.Value = (bool)e.Value ? CheckState.Checked : CheckState.Unchecked;
}
 
void binding_Parse(object sender, ConvertEventArgs e)
{
    e.Value = ((CheckState)e.Value) == CheckState.Checked ? true : false;
}

Let me know if you have further questions.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Jasper
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or