根据绑定数据格式化数据绑定控件

时间:2013-06-17 13:59:40

标签: c# winforms data-binding datarepeater

我使用以下内容替换转发器中的某些数据 -

        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;

如果选中cbSchoolFriday,我想将Picture Box pbFriday的背景颜色更改为红色。我怎么能这样做?

截图 - enter image description here

由于

2 个答案:

答案 0 :(得分:1)

如果您想在不使用DataBindings事件的情况下使用CheckedChanged,可以使用以下解决方案:

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);

我不确定您的dataRepeater.ItemTemplate是否有DataBindings属性,以便我们可以使用dataRepeater.ItemTemplate.DataBindings

答案 1 :(得分:0)

我设法解决了这个问题。看来dataRepeater有一个DrawItem事件。在这里,我输入以下代码 -

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;
        }
    }

感谢您的帮助。