【问题标题】:Is is possible to add a row to a binding source of the combo box which is data bound是否可以向数据绑定的组合框的绑定源添加一行
【发布时间】:2013-04-09 17:50:26
【问题描述】:

是否可以使用以下内容向绑定源添加一行:

"Please Select..."   <-- what it will display on the combobox
DbNull.Value   <-- Value

我有一个组合框,它是绑定到绑定源的数据。我不想向数据库添加额外的行,而是有一个选项只显示在组合框中,因此当它读取上述内容时,它会在数据库中设置相应的值。另外因为还有其他组合框使用相同的数据成员和数据源,所以我只想将选项添加到那个特定的组合框。

以上是设计器文件 InitializeComponent() 方法的内容

  this.cmbSecCSR = new System.Windows.Forms.ComboBox();
        this.csrBindingSource2 = new System.Windows.Forms.BindingSource(this.components);
 this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
// this.cmbSecCSR.Items.Insert(0, "Select");
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);

// csrBindingSource2
// 
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;

//

【问题讨论】:

  • 只需定义另一个源,该源仅引用另一个源并将该行插入位置 0。
  • 这个的初始化是在Designer.CS中完成的......应该在那里完成......??
  • 在您展示 CompboBox 所绑定的内容之前,无法提供具体信息。
  • “组合框绑定到什么”?什么是 csrBindingSource2 以及它是如何填充的。
  • CsrBindingSource 引用了一个 Datasheet ProductionDS,其中有一个名为 CSR 的数据表

标签: c# .net winforms data-binding


【解决方案1】:

试试这个示例方法。

标记:

<asp:DropDownList ID="ddljobcategory" CssClass="txtborder" Width="175px" runat="server" Height="20px" >
</asp:DropDownList>

代码隐藏:

DataView dvProduct = new DataView();
dvProduct = objcategory.GetCategoryType();
ddljobcategory.DataTextField = "CatName";
ddljobcategory.DataValueField = "CatId";
ddljobcategory.DataSource = dvProduct;
ddljobcategory.DataBind();
ddljobcategory.Items.Insert(0, "Select");//**Add your content instead of Select**
ddljobcategory.Items[0].Value = "0";

显示如下:

【讨论】:

  • 由于问题是指绑定我不认为它是 ASP.NET
【解决方案2】:

如果您使用 SQL 来创建 DataView UNION,请使用包含所需值的 SELECT 语句:

          SELECT Rule_Number, Title FROM Rules 
          UNION
          SELECT '', '<Select>'
          ORDER BY 2

【讨论】:

  • 这行不通,因为同一个 select 语句表用于其他绑定......而且还有多个值被选择并根据办公室位置等显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2012-09-19
  • 2013-08-11
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多