2017-10-17 65 views
-1

如何存储我的复选框列表的选中项目?我没有一个类,所以我不能使用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?     
} 
+0

你得到任何数据从您的数据库? – Sasha

+0

那么,你的代码真正的问题是什么? – Ferus7

+0

@Sasha,当我使用combox时,是的,我可以获取数据。但我会将其更改为清单,以便我可以选择多个数据。问题是我不知道如何获得检查的项目。我想如果我可以将字符串/ s存储到数组中并加入它。或者还有其他方法吗? –

回答

-1

也许这可以帮助你:

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); 
string[] chkList = new string[dSet.Tables[0].Rows.Count] 
for (int i = 0; i < dSet.Tables[0].Rows.Count; i++) 
{ 
    chkList[i]=dSet.Tables[0].Rows[i][0].ToString(); //This is the part where the items are populated 
} 
connection.Close(); 
+1

我猜chkList已经存在,这不会编译? – Ferus7

+0

请您指出*如何*和*为什么*这解决了OP的问题?只是粘贴一些代码并不能很好地解决问题。 –

+0

纠正我,如果我错了。 string [] chkList = new string [dSet.Tables [0] .Rows.Count]将创建一个数组的行数?如果项目被检查,这也会得到吗? –

相关问题