Question

I am displaing some data in a repeater using the following -

        BindingSource bindingSource5 = new BindingSource();
        bindingSource5.DataSource = BookingManager.GetBookingDaysForDayWithBookingFields(DateTime.Now.AddDays(+4));

        lbSchoolFri.DataBindings.Add("Text", bindingSource5, "SchoolName");
        lbTeacherFri.DataBindings.Add("Text", bindingSource5, "FullName");

        lbDurationFri.DataBindings.Add("Text", bindingSource5, "BookingDayDuration");
        lbYear.DataBindings.Add("Text", bindingSource5, "Year");

        cbSchoolFri.DataBindings.Add("Checked", bindingSource5, "SchoolContacted");
        cbTeacherFri.DataBindings.Add("Checked", bindingSource5, "TeacherContacted");
        cbEmailFri.DataBindings.Add("Checked", bindingSource5, "LetterSent");

        dataRepeater5.DataSource = bindingSource5;

If cbSchoolFriday is checked, I want to change the back colour of the Picture Box pbFriday to red. How can I do this?

Screenshot - enter image description here

Thanks

Was it helpful?

Solution 2

I have managed to resolve this. It appears the dataRepeater has a DrawItem event. Within here, I put the following code -

private void dataRepeater1_DrawItem(object sender, Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
    {
        CheckBox cbSchoolMon = e.DataRepeaterItem.Controls["cbSchoolMon"] as CheckBox;
        Label lbTeacherIDMon = e.DataRepeaterItem.Controls["lbTeacherIDMon"] as Label;

        PictureBox pbMon = e.DataRepeaterItem.Controls["pbMon"] as PictureBox;

        if (cbSchool.Checked)
        {
            pbMon.BackColor = Color.Red;
        }
        else
        {
            pbMon.BackColor = Color.Yellow;
        }
    }

Thanks for your help.

OTHER TIPS

If you want to play with DataBindings without using CheckedChanged event, here is the solution:

Binding bind = new Binding("Checked", bindingSource5, "SchoolContacted");
bind.Format += (s,e) => {
    e.Value = (int)e.Value == 1;
    dataRepeater.ItemTemplate.BackColor = ((bool)e.Value) ? Color.Red : Color.White;
};
cbSchoolFri.DataBindings.Add(bind);

I'm not sure if your dataRepeater.ItemTemplate has a DataBindings property so that we can use dataRepeater.ItemTemplate.DataBindings?

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