[英]How to retrieve checkedlistbox value from Database using C#
我想從ms訪問數據庫中獲取值到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();
}
任何幫助將不勝感激!
看一下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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.