C# 在checkListBox中获取值和项

C# 在checkListBox中获取值和项,c#,C#,我正在编写一个带有复选框的Windows窗体应用程序。我有一个连接到sql db的DataInded checkListBox值。我想写一个循环来循环检查项目列表,并获取其值(而不是索引)。我想知道有没有一种方法可以像comboBox.SelectedValue那样实现它 foreach(var item in checkListBox.CheckedItems){ //get the value of that string query = select * from em

我正在编写一个带有复选框的Windows窗体应用程序。我有一个连接到sql db的DataInded checkListBox值。我想写一个循环来循环检查项目列表,并获取其值(而不是索引)。我想知道有没有一种方法可以像comboBox.SelectedValue那样实现它

foreach(var item in checkListBox.CheckedItems){

    //get the value of that 
    string query = select * from employeeId where '"+checkListBox.SelectedValue+"'

}

您应该将项目强制转换为相关类型(DataRowView?)


你也可以用另一种方式

List<object> _checkedItems = checkedListBox1.CheckedItems.OfType<object>().ToList();
并将其传递到sql查询中

   string query = select * from employeeId where somevalue in ("+_selectedItems +")
您可以这样尝试:

foreach(object item in checkListBox.CheckedItems)
{
     DataRowView dt = item as DataRowView;
     string str = dt["nameHere"];
     // some code
}
你可以试试这个

foreach(var item in checkListBox.CheckedItems){

     var value = (item as ListItem).Value;
}

检查
选中列表框中的项目
,并检查列表中的每个其他项目。使用Items属性获取CheckedListBox.ObjectCollection以获取项目计数

使用和方法设置项目的检查状态。对于要检查的每个其他项,调用SetItemCheckState将CheckState设置为不确定,而对其他项调用SetItemChecked将CheckState设置为已检查

//checkedListBox1.SetItemChecked(a,true);

for ( int i = 0; i < checkedListBox1.Items.Count; i++ )
{
    if(checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
    {
        string query = select * from employeeId where '" + checkedListBox1.Items[i].ToString() + "';
    }

}
//checkedListBox1.SetItemChecked(a,true);
对于(int i=0;i
我可以用sql查询来做这件事吗?@DragonBorn:-我觉得用sql查询不是个好主意。我明白了。谢谢你的建议。我会给它一个try@DragonBorn:-如果可以将sql查询的结果绑定到DataTable,则可以将其强制转换为DataRowView,然后使用其Row属性获取DataRow。嗨,Roy,我只能找到ListViewItem
foreach(var item in checkListBox.CheckedItems){

     var value = (item as ListItem).Value;
}
//checkedListBox1.SetItemChecked(a,true);

for ( int i = 0; i < checkedListBox1.Items.Count; i++ )
{
    if(checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
    {
        string query = select * from employeeId where '" + checkedListBox1.Items[i].ToString() + "';
    }

}