Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#通过匹配字符串数组来选择复选框列表项_C#_Asp.net_String Matching_Selectedindex_Checklistbox - Fatal编程技术网

C#通过匹配字符串数组来选择复选框列表项

C#通过匹配字符串数组来选择复选框列表项,c#,asp.net,string-matching,selectedindex,checklistbox,C#,Asp.net,String Matching,Selectedindex,Checklistbox,我正在努力找到解决我问题的办法,我花了相当多的时间尝试其他的解决办法,但都无济于事。任何帮助或解释都将不胜感激 我有一个SQL数据库,它有一个名为“categories”的字符串字段,其中包含一个以“,”分隔的类别列表,例如转诊、门诊 因此,我希望这个列表与许多CheckListBox项目(ID=CategoryCBL)进行比较,只要类别与需要选择的CheckListBox项目相匹配 这是我的密码: string categories = result.GetString(12).ToStrin

我正在努力找到解决我问题的办法,我花了相当多的时间尝试其他的解决办法,但都无济于事。任何帮助或解释都将不胜感激

我有一个SQL数据库,它有一个名为“categories”的字符串字段,其中包含一个以“,”分隔的类别列表,例如转诊、门诊

因此,我希望这个列表与许多CheckListBox项目(ID=CategoryCBL)进行比较,只要类别与需要选择的CheckListBox项目相匹配

这是我的密码:

string categories = result.GetString(12).ToString();
string[] categorie = categories.Split(',');

 //loops through all seperated categories (cat) in categorie.
 foreach(string cat in categorie)
 {
     //loops through all list checkboxes 
     for(int index = 0; index <CategoryCBL.Items.Count; index++)
     {
         //gets the listcheck box string 
         string item = CategoryCBL.Items[index].ToString();
         //compare the list box string against the current Category looking for matches
         if (item == cat)
         {
             //if a match occures the list checkbox at that index is selected 
             CategoryCBL.SelectedIndex = index;

             TextBox1.Text += item + "-" + cat + "-" + index;
         }
     }
 }
string categories=result.GetString(12.ToString();
string[]categorie=categories.Split(',');
//循环通过Category中的所有分离类别(cat)。
foreach(分类中的字符串cat)
{
//循环浏览所有列表复选框

对于(int index=0;index我认为您需要这样的内容。您可以将字符串拆分为
列表
,然后使用Linq检查项目是否存在于
CategoryCBL
中,然后选中复选框

string categories = "Outcome Form, Executive Reporting, QlikView, Waiting Times";

List<string> categorie = categories.Split(',').ToList();

CategoryCBL.Items.Cast<ListItem>().ToList().ForEach(x => x.Selected = categorie.Any(y => y.Trim() == x.Value));
string categories=“结果表、执行报告、QlikView、等待时间”;
List categorie=categories.Split(',').ToList();
CategoryCBL.Items.Cast().ToList().ForEach(x=>x.Selected=category.Any(y=>y.Trim()==x.Value));
如果您需要文本框中的选中项,您可以这样做

TextBox1.Text = String.Join(", ", CategoryCBL.Items.Cast<ListItem>().Where(x => x.Selected).Select(y => y.Value).ToList());
TextBox1.Text=String.Join(“,”,CategoryCBL.Items.Cast()。其中(x=>x.Selected)。选择(y=>y.Value)。ToList());

为每个需要检查的项目将所选属性设置为true

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;
}
在代码中:

//loops through all list checkboxes 
 for(int index = 0; index <CategoryCBL.Items.Count; index++)
 {
     //gets the listcheck box string 
     string item = CategoryCBL.Items[index].ToString();
     //compare the list box string against the current Category looking for matches
     if (item == cat)
     {
         //if a match occures the list checkbox at that index is selected 
         CategoryCBL.Items[index].Selected= true;

         TextBox1.Text += item + "-" + cat + "-" + index;
     }
 }
//遍历所有列表复选框
对于(int index=0;index

tolgraph.Items.FindByText(“列”).Selected=true;

您需要在问题本身中共享您的代码。图像不可读。您如何用所有类别填充CategoriesCBL?“这是我的代码:”不,在stackoverflow上,代码是文本而不是图像,所以如果这与其他控件类似,那么您只能选择一个索引。列表中的复选框是否具有可以设置为true的IsChecked属性?谢谢您的回答!它起作用了。我以为我很接近了!@HarryNicholls Ya,不太远。我想应该是这样的,但我知道我很少使用asp.net,所以我花了一点时间才找到它。
//loops through all list checkboxes 
 for(int index = 0; index <CategoryCBL.Items.Count; index++)
 {
     //gets the listcheck box string 
     string item = CategoryCBL.Items[index].ToString();
     //compare the list box string against the current Category looking for matches
     if (item == cat)
     {
         //if a match occures the list checkbox at that index is selected 
         CategoryCBL.Items[index].Selected= true;

         TextBox1.Text += item + "-" + cat + "-" + index;
     }
 }
<asp:CheckBoxList ID="tolgraph" runat="server" RepeatLayout="Table" CssClass="cb" RepeatDirection="Horizontal">
    <asp:ListItem Text="Column" Value="column"></asp:ListItem>
    <asp:ListItem Text="Line" Value="line"></asp:ListItem>
    <asp:ListItem Text="Bar" Value="bar"></asp:ListItem>
    <asp:ListItem Text="Pie" Value="pie"></asp:ListItem>
    <asp:ListItem Text="Radar" Value="Radar"></asp:ListItem>
    <asp:ListItem Text="Pareto" Value="Pareto"></asp:ListItem>
</asp:CheckBoxList>

tolgraph.Items.FindByText("Column").Selected = true;