views:

86

answers:

2

Hi there, I have populate a CheckedListBox binding a DataSource with Display Member and Vlaue Member. I have saved the values of the checkedValues in Database with a Relational Table. I need to repopulate the checkedListbox getting the checked which value was checked(Saved) previously.I have done the First step. But in the Second step i am unable to get the index of the items to keep them checked comparing the relational table.

Following my code to bind the CheckedListBox:

private void FillCheckedListBox (CheckedListBox chkListBox, string sSql, string displayMember, string valueMember)
        {
            try {
                SqlConnection con = new SqlConnection(DaoCodeGCommon.GetConstring());
                con.Open();
                DataTable accTable = new DataTable();
                SqlCommand cmd = new SqlCommand(sSql, con);
                SqlDataAdapter adpObj = new SqlDataAdapter(cmd);
                accTable.TableName = "tbl";
                adpObj.Fill(accTable);
                con.Close();
                chkListBox.DataSource = accTable;
                chkListBox.ValueMember = valueMember;
                chkListBox.DisplayMember = displayMember;
            }
            catch (Exception ex) {

                throw ex;
            }
        }

Is there any way to repopulate the checkedListBox?

A: 

If you have the status in the database and you are pulling the data from the db you can easily reload the check box list.

For instance assume you load a check box list with some sort of reader:

chkUsers.DataSource = reader;
chkUsers.DataTextField = "YourTextFieldToView";
chkUsers.DataValueField = "YourValueFieldToHide";
chkUsers.DataBind();

So now you've loaded the check list box, and now you need to turn the check boxes on / off.

for(int i =0; i< chkUsers.Items.Count-1; i++)
 {
  if(dbVal == chkUsers.Items(i).Value)
   {
    chkUsers.Items(i).Selected = dbVal;
   }
  }

In this case dbVal may be some value in a reader or dataset or datatable etc.

JonH
A: 

JonH >>

I really dishearted after reading the JonH's Comments. I dont know why u think that i said anybody not to post the solution in the forum. I just sent a request to sent me mail. if any wants, he/she can do that (if he/she has the mentality to help by email). Actually I know some forum where email is sent automatically when any answer is posted. After reading the comments I thought that you might post an answer in the forum. But so far ......

Any way if you have the capacity, please try to forgive for this kind of request.

So far the solutions which are posted, i can't solve the problem.

Farazi