-1

How can I store the checked items of my checkbox list? I dont have a class so I can't use ListItems. My checkbox list is populated using my database (MS Access).

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();

edit using foreach

foreach (string chk in chkList.CheckedItems)
{
    MessageBox.Show(chk.ToString()); //This gets every string checked, but one after another
    top = chk;
}

for my query

"SELECT TOP " + num + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT FROM " + tab + " WHERE CONTENT = '" + top + "'"

it only query the last item checked.

edit to count the items checked

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?                
}
  • Do you get any data from your db? – Sasha Oct 17 '17 at 06:40
  • So, what is the real problem with your code? – Ferus7 Oct 17 '17 at 06:42
  • @Sasha, when I'm using combox, yes i can get data. But I will change it to checklist so I can choose multiple data. Problem is I don't know how can I get the checked items. I'm think if I can store the string/s into an array and join it. Or is there any other way? – Jepher John Chang Oct 17 '17 at 06:48
  • @Ferus7, The code posted doesn't have a problem. But it is still lacking to how can I get the checked items. I'm lost on where can I add the code to get the checked items. – Jepher John Chang Oct 17 '17 at 06:49
  • @JepherJohnChang Where do you want to get them? Can you show where exactly error is? – Sasha Oct 17 '17 at 06:52
  • @Sasha, Im using a local database. I'm lost in constructing the code for my problem. Since I dont know to get the value of the checked item/s – Jepher John Chang Oct 17 '17 at 07:15
  • In ASP.NET you need to loop the items https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#Remarks while in WinForm you can directly call CheckedItems https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.checkedlistbox.checkeditems?view=netframework-4.7#System_Windows_Forms_CheckedListBox_CheckedItems – Martheen Oct 17 '17 at 07:15
  • @JepherJohnChang what kind of project do you have? – Sasha Oct 17 '17 at 07:44
  • im using winform. – Jepher John Chang Oct 17 '17 at 07:49
  • @JepherJohnChang have a look here https://stackoverflow.com/questions/32646757/c-sharp-checkedlistbox-multicolumn and here https://stackoverflow.com/questions/9951704/add-item-to-listview-control – Sasha Oct 17 '17 at 07:54
  • @Sasha. what i want is i need to get the strings of the checked items. so i can use it in a query like this. "SELECT TOP " + num + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT FROM " + tab + " WHERE CONTENT = '" + top + "'". The top is equal to the checked items from my checkbox. – Jepher John Chang Oct 17 '17 at 09:48
  • @Martheen, i have used that. but i can only get the last string of the last item checked in my checkboxlist. – Jepher John Chang Oct 17 '17 at 09:49
  • @JepherJohnChang if you are using ListView try this: myListView.CheckedItems – Sasha Oct 17 '17 at 10:03
  • @JepherJohnChang i have used that. but i can only get the last string of the last item checked in my checkboxlist. - can you edit your question and show this part? – Sasha Oct 17 '17 at 10:07
  • @Sasha, i have edited my post. – Jepher John Chang Oct 17 '17 at 10:15
  • @JepherJohnChang Where do you add values to num array? – Sasha Oct 17 '17 at 10:28
  • So if the selected items are 'a', 'b', 'c', you want top to be "'a', 'b', 'c'"? Because you can't use = for that in T-SQL, you'll need IN ('a', 'b', 'c'). https://learn.microsoft.com/en-us/sql/t-sql/language-elements/in-transact-sql – Martheen Oct 17 '17 at 10:37
  • @Sasha, SELECT TOP " + num + ? this one? if yes, don't mind this part. if no, i edited again my post. – Jepher John Chang Oct 17 '17 at 10:40
  • Use Join https://stackoverflow.com/q/4841401/529282 to combine the array into single string – Martheen Oct 17 '17 at 10:41
  • @JepherJohnChang Have a look here https://technet.microsoft.com/de-de/library/system.windows.forms.listview.checkeditems(v=vs.85).aspx – Sasha Oct 17 '17 at 10:59
  • @Martheen, i tried using for loop but i dont know how to assign the text for each index. i tried using this. topic = topic + ", " + chk; or topic = chk + ", "+ topic;. but i can't remove the extra , – Jepher John Chang Oct 17 '17 at 11:37
  • Removing the last character is straightforward https://stackoverflow.com/a/3573298/529282 But I STILL don't think you want = ('a,b,c'), you'll most likely want IN ('a','b','c') – Martheen Oct 18 '17 at 08:33
  • i ended up using list. but i cant get the first item that ia checked – Jepher John Chang Oct 18 '17 at 08:44

1 Answers1

-1

Maybe this can help you:

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();
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
Hamed
  • 7
  • 1
  • Could you please point out *how* and *why* this solves the OPs issue? Just pasting some code does not make a good answer. – Paul Kertscher Oct 17 '17 at 06:41
  • correct me if im wrong. string[] chkList = new string[dSet.Tables[0].Rows.Count] will create an array of rows counted? Will that also get if the item is checked? – Jepher John Chang Oct 17 '17 at 06:45
  • in this code we get a string array for store your data in this array if you want store your data in array this can help you . else your mean not this say me! – Hamed Oct 17 '17 at 06:50
  • How making chkList local should help him? – Sasha Oct 17 '17 at 07:00