如何在GridView中绑定checkboxlist

时间:2012-12-05 10:45:12

标签: c# asp.net telerik-grid

我有 GridView 有一列是 CheckBoxList(周一,周二,周三,周四,周五,周六,周日)

所选周的数据:

  • “1101000”表示(Mon,Tues,Thur被选中)
  • “1000000”表示(选择周一)
  • “0100000”表示(选择周二)

以下用于识别所选项目

            Boolean isMonday = false;
            Boolean isTuesday = false;
            Boolean isWednesday = false;
            Boolean isThursday = false;
            Boolean isFriday = false;
            Boolean isSaturday = false;
            Boolean isSunday = false;

            if (alertDayInt >= 1000000)
            {
                isMonday = true;
                alertDayInt -= 1000000;
            }
            else if (alertDayInt >= 100000)
            {
                isTuesday = true;
                alertDayInt -= 100000;
            }
            else if (alertDayInt >= 10000)
            {
                isWednesday = true;
                alertDayInt -= 10000;
            }
            else if (alertDayInt >= 1000)
            {
                isThursday = true;
                alertDayInt -= 1000;
            }
            else if (alertDayInt >= 100)
            {
                isFriday = true;
                alertDayInt -= 100;
            }
            else if (alertDayInt >= 10)
            {
                isSaturday = true;
                alertDayInt -= 10;
            }
            else if (alertDayInt >= 1)
            {
                isSunday = true;
                alertDayInt -= 1;
            }

2 个答案:

答案 0 :(得分:1)

假设这些字符串是您想要转换为CheckBoxList选项的可能输入数据。 Linq:

var sampleData = new[]{ "110100", "100000", "010000" };
IEnumerable<IEnumerable<DayOfWeek>> selectedDays = sampleData
            .Select(str => 
                str.Select((c, i) => new { Selected = c == '1', Value = i+1 })
                   .Where(x => x.Selected)
                   .Select(x => (DayOfWeek)x.Value));

现在,您只需设置Selected中每个ListItem的{​​{1}}属性:

CheckBoxList

假设ListItem的Text是英文日期名称。最好将DayOfWeek枚举的var firstSample = selectedDays.First(); foreach(ListItem item in CheckBoxList1.Items) item.Selected = firstSample.Any(day => day.ToString() == item.Text); 值用作int

Value

答案 1 :(得分:1)

List<string> selectedItemsDays = new List<string> { };
            if (alertDayInt >= 1000000)
            {
                selectedItemsDays.Add("Mon");
                alertDayInt -= 1000000;
            }
            if (alertDayInt >= 100000)
            {
                selectedItemsDays.Add("Tue");
                alertDayInt -= 100000;
            }
            if (alertDayInt >= 10000)
            {
                selectedItemsDays.Add("Wed");
                alertDayInt -= 10000;
            }
            if (alertDayInt >= 1000)
            {
                selectedItemsDays.Add("Thu");
                alertDayInt -= 1000;
            }
            if (alertDayInt >= 100)
            {
                selectedItemsDays.Add("Fri");
                alertDayInt -= 100;
            }
            if (alertDayInt >= 10)
            {
                selectedItemsDays.Add("Sat");
                alertDayInt -= 10;
            }
            if (alertDayInt >= 1)
            {
                selectedItemsDays.Add("Sun");
                alertDayInt -= 1;
            }
相关问题