1

私はGridViewを持っています 1 つの列はCheckBoxList(月、火、水、木、金、土、日) です

選択した週のデータ:

  • 「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
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 に答える
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のそれぞれのプロパティを設定するために必要なすべてが揃いました。ListItemCheckBoxList

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 に答える