简体   繁体   中英

How To Retrieve Checkedlistbox Value From Database

I need to fetch data from datagridview to checkedlistbox while i click on modify button in C#.

data will be already stored in database during insert operation, while updating i need to fetch the data from database to checkedlistbox, and the data should be checked.

Data from database should compare with data in checkedlistbox, if both equals then it should be checked.

string category = dgEntry.CurrentRow.Cells[8].Value.ToString();
string[] strCat = category.Split(',');
int length = strCat.Length;
for (int i = 0; i < length ; i++)
{
    string fetch = strCat[i];
    foreach (object item in clbEntry_IndividualItems.Items)
    {
        DataRowView row = item as DataRowView;
        if (row[0].ToString() == strCat[i].ToString())
        {
            clbEntry_IndividualItems.SetItemChecked(i, true);
        }
    }
}

Please help me solve this issue.

if your first check is true (as your comment) then you can add 2nd loop to check all:

  string category = dgEntry.CurrentRow.Cells[8].Value.ToString();
    string[] strCat = category.Split(',');
    int length = strCat.Length;
    int _lenghth =clbEntry_IndividualItems.Items.count();
    for (int j=0, j<_lenghth ,j++){
    for (int i = 0; i < length ; i++)
    {
        string fetch = strCat[i];
        foreach (object item in clbEntry_IndividualItems.Items)
        {
            DataRowView row = item as DataRowView;
            if (row[j].ToString() == strCat[i].ToString())
            {
                clbEntry_IndividualItems.SetItemChecked(i, true);
            }
        }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM