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();
我正在尝试将默认值添加到已填充数据源的组合框中。检查下图:
值是从 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();