0

I have CheckBoxList I want to Check multiple items based on database value here is my code

String TargetQuery = "Select RoleID from t_Et_Role_Staff_Combination where  EmployeeID='" + EmployeeID + "'";    
SqlDataAdapter adapt = new SqlDataAdapter(TargetQuery, DBcon.con);        
DataTable dtcheck = new DataTable();
adapt.Fill(dtcheck);

protected void Page_Load(object sender, EventArgs e)
{    
    for (int i = 0; i < dtcheck.Rows.Count; i++)
    {    
        CheckBoxList1.SelectedValue = dtcheck.Rows[i][0].ToString();        
    }
}

but this checked only one item( the last item) from DataTable the previous items are not selected

[based on my database value administrator and aaa should be checked but now only aaa is selected]

enter image description here

4
  • CheckBoxList1.SelectedValue for each iteration of your for loop you are setting value to the same CheckBox whose name is CheckBoxList1 Commented Nov 16, 2015 at 8:34
  • Every CheckBoxList item has a Boolean flag Selected. Use this. Commented Nov 16, 2015 at 8:35
  • thank you but its Checkboxlist not checkbox. it has multiple items. i want to check by item value Commented Nov 16, 2015 at 8:39
  • Is your Checkboxlist selection mode is MultiExtended or single ? Commented Nov 16, 2015 at 9:12

1 Answer 1

1

You need to iterate inside Checkboklist items not in datatable.

if (dtcheck != null && dtcheck.Rows.Count > 0) /* Prevents from null reference exception */
{   
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if ( i < dtcheck.Rows.Count) /* Ensure and prevents from Index out of range exception */
        {
            bool flag = default(bool);
            Boolean.TryParse(dtcheck.Rows[i][0].ToString(), out flag);
            CheckBoxList1.Items[i].Selected = flag;
        }
    }
}

Since you have updated your post and rest of detail above mentioned code won't work in that case. As mentioned your database value is administrator but your checkboxlist text is Administrator both are different you need something like following that matches the text and ignore the case.

for (int i = 0; i < dtcheck.Rows.Count; i++)
{    
    string compareText = dtcheck.Rows[i][0].ToString();
    var vCheckBoxItem = CheckBoxList1.Items.Cast<ListItem>()
                        .FirstOrDefault(x => x.Text.Equals(compareText, StringComparison.CurrentCultureIgnoreCase));
    if (vCheckBoxItem != null)
        vCheckBoxItem.Selected = true;      
}
Sign up to request clarification or add additional context in comments.

5 Comments

its not working CheckBoxList1.Items[i].Checked = flag; .checked is not recognize.
@user3536676: After seeing your updated code I have updated my post. Try it, Hope this works for you.
Sorry, It's not Checked actually it's Selected.
thank you still not working but i used value not Items Name
Can you just share markup for checkboxlist and how you are binding checkboxlist and what data does datatable have?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.