获取CheckBoxList项目值 [英] Getting CheckBoxList Item values
问题描述
我有一个CheckBoxList,其中装有数据.当我尝试从列表中检索选中的项目时,我只能按顺序抓取该项目,而无法获取该值.
I have a CheckBoxList which I'm populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.
我已经读到您可以使用Items [i] .Value,但是当我尝试执行此操作时,我收到一条错误消息,指出没有扩展方法'value'.
I've read that you can use Items[i].Value however when I try to do this I get an error stating that there is no extension method 'value'.
这是我用来尝试获取信息的代码(请注意,GetItemText(i)实际上只给了我项目的位置,而不是项目的文本)
Here's the code I'm using to try and grab the information (note the GetItemText(i) actually only gives me the item position, not the text for the item)
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = chBoxListTables.GetItemText(i);
MessageBox.Show(str);
//next line invalid extension method
chBoxListTables.Items[i].value;
}
}
}
这是使用.Net 4.0
This is using .Net 4.0
任何想法都会受到赞赏...谢谢
Any thoughts would be appreciated...thanks
推荐答案
这最终变得非常简单.chBoxListTables.Item [i]是一个字符串值,并且通过显式转换将其加载到变量中.以下代码有效:
This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable. The following code works:
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = (string)chBoxListTables.Items[i];
MessageBox.Show(str);
}
}
}
这篇关于获取CheckBoxList项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!