如何存储我的复选框列表的选中项目?我没有一个类,所以我不能使用ListItems。我的复选框列表填充使用我的数据库(MS Access)。如何将复选框项目存储到C#中的字符串数组?
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @"SELECT CONTENT FROM " + tab + " GROUP BY CONTENT ORDER BY MIN(CHAP)";
OleDbDataAdapter dAdap = new OleDbDataAdapter(command);
DataSet dSet = new DataSet();
dAdap.Fill(dSet);
for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
{
chkList.Items.Add(dSet.Tables[0].Rows[i][0].ToString()); //This is the part where the items are populated
}
connection.Close();
编辑使用的foreach
foreach (string chk in chkList.CheckedItems)
{
MessageBox.Show(chk.ToString()); //This gets every string checked, but one after another
top = chk;
}
我查询
"SELECT TOP " + num + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT FROM " + tab + " WHERE CONTENT = '" + top + "'"
它只能查询最后一项检查。
编辑计数物品托运
MessageBox.Show(chkList.CheckedItems.Count.ToString());
string[] topic = new string[chkList.CheckedItems.Count];
for (int i = 0; i < topic.Length; i++)
{
topic[i] = //How to get the text for each checked items?
}
你得到任何数据从您的数据库? – Sasha
那么,你的代码真正的问题是什么? – Ferus7
@Sasha,当我使用combox时,是的,我可以获取数据。但我会将其更改为清单,以便我可以选择多个数据。问题是我不知道如何获得检查的项目。我想如果我可以将字符串/ s存储到数组中并加入它。或者还有其他方法吗? –