开发者

CheckedListBox Repopulating problem in C#.NET

开发者 https://www.devze.com 2022-12-18 17:32 出处:网络
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 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 b开发者_Go百科ind 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消