1

我有GridView有一列是CheckBoxList(Mon, Tues, Wed, Thur, Fri, Sat, Sun)

选定周的数据:

  • “1101000”的意思是(周一、周二、周四被选中)
  • “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;
            }
4

2 回答 2

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一切CheckBoxList

var firstSample = selectedDays.First();
foreach(ListItem item in CheckBoxList1.Items)
    item.Selected = firstSample.Any(day => day.ToString() == item.Text); 

假设 ListItem 的 Text 是英文日名。使用枚举的int值可能会更好:DayOfWeekValue

firstSample.Any(day => (int)day == int.Parse(item.Value));

于 2012-12-05T11:11:23.463 回答
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;
            }
于 2013-01-02T07:07:26.357 回答