2016-06-20 5 views
0

abrufen Ich möchte den Wert von ms Access-Datenbank auf die checkedListBox erhalten. funktioniert es richtig für die ComboBox und TextBox, aber ich weiß nicht, wie man das mit der checkedListBox_prodline oder checkedListBox_owner. (Ich habe nur einen Wert im Datenbankfeld)Wie Checklistbox-Wert aus der Datenbank mit C#

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

Jede mögliche Hilfe würde sehr geschätzt werden!

Antwort

1

Werfen Sie einen Blick auf CheckedListBox.SetItemChecked. Wenn Ihre Artikel Saiten sind.

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

Das war schnell. :) Es klappt! Danke vielmals. – NOGRP90

+0

Ja, CheckedIndexCollection.Clear löst NotSupportedException aus, wenn Sie darauf bestehen, anzurufen. – Tommy

+0

Darf ich noch eine Frage haben? Weißt du, wie kann ich das auch für die Radioknöpfe tun? – NOGRP90

Verwandte Themen