checked list box

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

I fill a checked listbox with name from dataset.

I would like to go through that names and if I find the same name in list,
then that line should be checked.

How?

Hrcko
 
Hi Hrvoje,

I feel something is missing from your question.

In any case, the ListBox will be filled with DataRowViews, so simply go through all DataRowViews in the ListBox.Items collection and if the row qualifies, check the appropriate ListBox item

for(int i = 0; i < listBox1.Items.Count; i++)
{
DataRowView d = (DataRowView)listBox1.Items;
if(d["Column1"].ToString() == "SomeValue")
listBox1.SetItemChecked(i, true);
}

Note that you cannot do this in the constructor in the parent control (form) as SetItemChecked will be ignored. You can however do it in the Form's Load event.
 
It doesn't work.

I get an error on DataRowView line : Specified cast is not valid.

Maybe I should put it somewhere in listBox control properties...


Morten Wennevik said:
Hi Hrvoje,

I feel something is missing from your question.

In any case, the ListBox will be filled with DataRowViews, so simply go
through all DataRowViews in the ListBox.Items collection and if the row
qualifies, check the appropriate ListBox item

for(int i = 0; i < listBox1.Items.Count; i++)
{
DataRowView d = (DataRowView)listBox1.Items;
if(d["Column1"].ToString() == "SomeValue")
listBox1.SetItemChecked(i, true);
}

Note that you cannot do this in the constructor in the parent control
(form) as SetItemChecked will be ignored. You can however do it in the
Form's Load event.

I fill a checked listbox with name from dataset.

I would like to go through that names and if I find the same name in
list,
then that line should be checked.

How?

Hrcko
 
Hm, you said you used a DataSet(DataTable) as DataSource? If so, there should be DataRowViews in the ListBox. In any case, you can check the type using Breakpoints or MessageBox.Show(listBox1.Items[0].GetType().ToString())


It doesn't work.

I get an error on DataRowView line : Specified cast is not valid.

Maybe I should put it somewhere in listBox control properties...


Morten Wennevik said:
Hi Hrvoje,

I feel something is missing from your question.

In any case, the ListBox will be filled with DataRowViews, so simply go
through all DataRowViews in the ListBox.Items collection and if the row
qualifies, check the appropriate ListBox item

for(int i = 0; i < listBox1.Items.Count; i++)
{
DataRowView d = (DataRowView)listBox1.Items;
if(d["Column1"].ToString() == "SomeValue")
listBox1.SetItemChecked(i, true);
}

Note that you cannot do this in the constructor in the parent control
(form) as SetItemChecked will be ignored. You can however do it in the
Form's Load event.

I fill a checked listbox with name from dataset.

I would like to go through that names and if I find the same name in
list,
then that line should be checked.

How?

Hrcko
 
Back
Top