checked list box

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
 
M

Morten Wennevik

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.
 
H

Hrvoje Voda

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
 
M

Morten Wennevik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

checked item 1
checked listbox check all 3
list box checked 2
list box checked names 5
ckecked listbox 4
list box fill 3
checkedlistbox 5
listbox search 1

Top