user1865039
user1865039

Reputation: 131

How to bind checkboxlist in GridView

i have GridView have one column is CheckBoxList(Mon, Tues, Wed, Thur, Fri, Sat, Sun)

Data for selected Week:

Below is use to identify the selected item

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

Upvotes: 1

Views: 744

Answers (2)

Louisth
Louisth

Reputation: 726

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

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460058

Assuming that those strings are possible input data you want to convert to a CheckBoxList selection. With 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));

Now you have all you need to set the Selected property of each ListItem in the CheckBoxList:

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

Assuming that the ListItem's Text is the english dayname. It's probably better to use the int value of the DayOfWeek enumeration as Value:

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

Upvotes: 1

Related Questions