Sandip's Programming Zen

An attempt to share tech/coding experiences

How to: Set checked property of CheckedListBox using code

leave a comment »

CheckedListBox does not have a straight forward way of making checkbox selected using code, so here is some code which may save some Google searches.

For example if you have a data bound CheckedListBox and you need to make some of the items selected by iterating through them then we can do as follows:


For Each dtr As DataRow In dtpack.Rows
For i As Integer = 0 To cmbServices.Items.Count - 1
If DirectCast(cmbServices.Items(i), DataRowView)("ID") = dtr("ID") Then
cmbServices.SetItemCheckState(i, CheckState.Checked)
End If
Next
Next

For Each dtr As DataRow In dt.Rows

For i As Integer = 0 To clbServices.Items.Count - 1

If DirectCast(clbServices.Items(i), DataRowView)("ID") = dtr("ID") Then

clbServices.SetItemCheckState(i, CheckState.Checked)

End If

Next

Next

Advertisements

Written by Sandip

September 12, 2009 at 7:27 am

Posted in .Net, Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: