簡體   English   中英

C# - 向填充了數據綁定的組合框添加值?

[英]C# - Adding value to combobox filled with databinding?

我正在嘗試向已填充數據源的組合框添加默認值。 檢查下面的圖像:

窗體

值是從 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";

我找到了一個解決方案,無法將項目添加到數據綁定的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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM