如何使用C#

时间:2016-06-20 15:49:15

标签: c# checkedlistbox

我想从ms access数据库获取值到checkedListBox。 它适用于ComboBox和TextBox,但我不知道如何使用 checkedListBox_prodline checkedListBox_owner 执行此操作。 (我在数据库字段中只有一个值)

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();
        }            

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

看看CheckedListBox.SetItemChecked。如果您的商品是字符串。

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);
}