2016-01-13 95 views
0

我試圖將默認值添加到已填充數據源的組合框中。檢查圖像下方:C# - 爲填充數據綁定的組合框添加值?

Windows form

值從Oracle數據庫自動添加。現在我需要做的是再添加一個默認值,它將代表所有類別。

這裏是一個設計師代碼:

this.comboBox2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.vRSTEPROBLEMABindingSource, "NAZIV", true)); 
     this.comboBox2.DataSource = this.vRSTEPROBLEMABindingSource1; 
     this.comboBox2.DisplayMember = "NAZIV"; 
     this.comboBox2.FormattingEnabled = true; 
     this.comboBox2.Location = new System.Drawing.Point(89, 51); 
     this.comboBox2.Name = "comboBox2"; 
     this.comboBox2.Size = new System.Drawing.Size(173, 21); 
     this.comboBox2.TabIndex = 4; 
     this.comboBox2.Text = "Svi"; 
     this.comboBox2.ValueMember = "NAZIV"; 
+0

在將數據源設置爲綁定源之前,您需要添加默認值。 – msmolcic

+0

我該怎麼做? – exort

回答

0

我找到了解決方法,有沒有辦法將項目添加到combobox綁定數據。你可以做的是將項目填入List然後將它們添加到組合框。這裏是一個代碼:

  // your connection string + open connection 
      OracleConnection conn = new OracleConnection(global::IssueTracker.Properties.Settings.Default.ConnectionString); 
      conn.Open(); 

      // data adapter 
      OracleDataAdapter oa = new OracleDataAdapter("select naziv from vrsteproblema", conn); 
      DataSet ds = new DataSet(); 
      oa.Fill(ds, "vrsteproblema"); 

      // put entries into a list 
      List<string> comboNazivi = new List<string>(); 
      foreach (DataRow row in ds.Tables["vrsteproblema"].Rows) 
      { 
       comboNazivi.Add(row["naziv"].ToString()); 
      } 

      // add custom entry 
      comboBox2.Items.Add("Svi"); 
      // fill the rest from the list 
      for (var i = 0; i < comboNazivi.Count; i++) { 
       comboBox2.Items.Add(comboNazivi[i]); 
      } 

      // dont forget to close conn 
      conn.Close();