簡體   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