简体   繁体   中英

How to retrieve checkedlistbox value from Database using C#

I would like to get the value from ms access database to the checkedListBox. it works properly for the ComboBox and TextBox but I don't know how to do that with the checkedListBox_prodline or checkedListBox_owner . (I have only one value in the database field)

private void button_clone_Click(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "SELECT * from PPAPdatabase where [PSW ID]=" + txt_c_PSW_ID.Text + "";
            OleDbDataReader dr = null;
            dr = command.ExecuteReader();                
            while (dr.Read())
            {
                comboBox_PPAP.Text = (dr["Reason"].ToString());
                checkedListBox_prodline.Text = (dr["Production Line"].ToString());                    
                checkedListBox_owner.Text = (dr["Owner"].ToString());              
                txt_comment.Text = (dr["Comment"].ToString());                                                          
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error has occurred: " + ex.Message,
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1);
        }
        finally
        {
            connection.Close();
        }            

Any help would be greatly appreciated!

Take a look of CheckedListBox.SetItemChecked. In case your items are strings.

var productLine = dr["Production Line"].ToString();
for (var i = 0; i < checkedListBox_prodline.Items.Count; i++)
{
    var item = checkedListBox_prodline.Items[i] as string;
    checkedListBox_prodline.SetItemChecked(i, productLine == item);
}

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