android listview chechbox单选,.net c#在checkListBox中获取值和项目

I am writing a Windows Forms application that has a checkListBox. I have a databinded checkListBox value that is connected to my sql db. I want to write a loop to loop through a list of checked items and get its value (not index). I am wondering is there a way to do it just like the comboBox.SelectedValue?

foreach(var item in checkListBox.CheckedItems){

//get the value of that

string query = select * from employeeId where '"+checkListBox.SelectedValue+"'

}

Solutions1

You can try like this:

foreach(object item in checkListBox.CheckedItems)

{

DataRowView dt = item as DataRowView;

string str = dt["nameHere"];

// some code

}

Talk1:

can I do this with sql query?

Talk2:

:- I don't think it would be a good idea to do it using sql query.

Talk3:

I see. thanks for your suggestion. I will give it a try

Talk4:

:- If you can bind the result of your sql query to a DataTable then you can cast it to a DataRowView and then take its Row property to get a DataRow.

Solutions2

You should cast the item to the relevant type (DataRowView?)

foreach(var item in checkListBox.CheckedItems){

var val = item as DataRowView;

// retrieving the relevant values

}

Solutions3

You can try this

foreach(var item in checkListBox.CheckedItems){

var value = (item as ListItem).Value;

}

Talk1:

Hi Roy, I can only find ListViewItem though

Solutions4

The items in the CheckedListBox and checks every other item in the list. Using the Items property to get the CheckedListBox.ObjectCollection to get the Count of items.

Using the SetItemCheckState and SetItemChecked methods to set the check state of an item. For every other item that is to be checked, SetItemCheckState is called to set the CheckState to Indeterminate, while SetItemChecked is called on the other item to set the checked state to Checked.

//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() + "';

}

}

Solutions5

You can do it other way as well

List _checkedItems = checkedListBox1.CheckedItems.OfType().ToList();

This will give you all the checked items. If you want to pass this into sql query then you can do something like

string delimeter = "','";

string _selectedItems ="'"+ _checkedItems.Aggregate((i, j) => i + delimeter + j).ToString()+"'";

and pass it in your sql query

string query = select * from employeeId where somevalue in ("+_selectedItems +")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值