繁体   English   中英

可以在数据绑定的组合框的绑定源中添加一行

[英]Is is possible to add a row to a binding source of the combo box which is data bound

是否可以使用以下命令将行添加到绑定源:

"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;

// 检查这张图片

尝试这种示例方式。

标记:

<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";

它显示如下:

在此处输入图片说明

在此处输入图片说明

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM