Click here to Skip to main content
15,918,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two checkedlistbox controls
1. chklst_type
2.chklst_test


chklst_type I fill through database.
On Checking of an item from chklst_type, I fill second checkedlistbox control i.e.(chklst_test)

I want to uncheck the last checked item from chklst_type and also clear the related items from the second checkedlistbox control (chklst_test).
I want to toggle it.

Means at a time only one checkbox should be checked from first control and its related items are shown in the second control.

Here is my code...




private void chklst_type_ItemCheck(object sender, ItemCheckEventArgs e)
{
            try
            {
                int j;
                for (j = 0; j <= (chklst_type.Items.Count - 1); j++)
                {
                    if (chklst_type.GetItemCheckState(j)==CheckState.Unchecked)
<pre>
{
id = Convert.ToInt32(chklst_type.SelectedIndex);
id = id + 1;
sqlQry = "select Lab_Test from LabTest where Type_Id='" + id + "'";
adapter = new SqlDataAdapter(sqlQry, Connection.sqlconn);
ds = new DataSet();
adapter.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
chklst_test.Items.Add(ds.Tables[0].Rows[i]["Lab_Test"].ToString());
}
}
}
}
catch (Exception ex)
{
Connection.m_MessageLog.Log("frmRecLabTest.cs", 57, 02636, 'U', 'E', 0, "chklst_type_ItemCheck:" + ex.ToString());
}
}
Posted
Updated 19-Mar-10 22:10pm
v2

1 solution

Assuming that chklst_type_ItemCheck is a handler for the checklistbox, why are you trying to reload the listbox?

C#
sqlQry = "select Lab_Test from LabTest where Type_Id='" + id + "'";
                        adapter = new SqlDataAdapter(sqlQry, Connection.sqlconn);
                        ds = new DataSet();
                        chklst_test.Items.Clear();
                        adapter.Fill(ds);
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            chklst_test.Items.Add(ds.Tables[0].Rows[i]["Lab_Test"].ToString());
                        }


All you need to is uncheck all items except the one that was checked. You don't need to reload anything.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900