从db中检索值并在c#中的checkboxlist中检查它们

时间:2018-02-06 21:33:18

标签: c# sql checkboxlist

我已经将checkboxlist中的值作为分隔到数据库中的字符串插入但由于某种原因,当我从数据库中选择它们时,我无法重新检查这些值。有人可以帮我吗?

            string category = dt.Rows[0][14].ToString();
            string[] strCat = category.Split(',');

            for (int i = 0; i < strCat.Length; i++)
            {
                for (int j = 0; j < checkCat.Items.Count; j++)
                {
                    if (checkCat.Items[j].Text == strCat[i])
                    {
                        checkCat.Items[j].Selected=true;
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

将它们放在一个小组中

        string category = dt.Rows[0][14].ToString();
        string[] strCat = category.Split(',');

        for (int i = 0; i < strCat.Length; i++)
        {

            foreach (CheckBox box in panel.Controls.OfType<CheckBox>())
            {
                if(box.Text == strCat[i])
                {
                  box.Checked = true; 
                }
            }
        }
相关问题