【问题标题】:How to bind checkboxlist in GridView如何在 GridView 中绑定复选框列表
【发布时间】:2012-12-05 10:45:12
【问题描述】:

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

【问题讨论】:

  • 为什么7天有6位?
  • 是 7 7 天 1000000 意味着星期一被选中

标签: c# asp.net telerik-grid


【解决方案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;
            }

【讨论】:

    【解决方案2】:

    假设这些字符串是您想要转换为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));
    

    现在您已经拥有了在CheckBoxList 中设置每个ListItemSelected 属性所需的一切:

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

    假设 ListItem 的 Text 是英文日名。将DayOfWeek 枚举的int 值用作Value 可能会更好:

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

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      相关资源
      最近更新 更多