在组合框中添加项目

时间:2013-02-12 03:22:04

标签: vb.net winforms combobox datatable

我想将一个项目添加到已经与某些数据绑定的combobox

代码:

Public Sub showSection()
        sb = New StringBuilder()
        sb.Remove(0, sb.Length)
        sb.Append("SELECT DISTINCT Section ")
        sb.Append(" FROM Employee ")
        sb.Append(" ORDER BY Section")
        Dim sqlSection As String = sb.ToString()

        da = New SqlDataAdapter(sqlSection, Conn)
        da.Fill(ds, "Section")

        dt = ds.Tables("Section")
        bs.DataSource = dt

        With cbSection
            .DisplayMember = "Section"
            .ValueMember = "Section"
            .DataSource = ds.Tables("Section")
            .DataBindings.Add("SelectedValue", bs, "Section")
        End With
End Sub

但是我想添加项目,比如"---All---",所以这应该是输出。

---All---
HR
Store
Packing
Training
Qc
Qa
Stock

1 个答案:

答案 0 :(得分:1)

这是简单的解决方案

Dim dr As DataRow = dt.NewRow()
dr("Section") = "---All---"
dr("SectionId") = 0
dt.Rows.InsertAt(dr, 0)

With cbSection
    .DisplayMember = "Section"
    .ValueMember = "SectionId"
    .DataSource = ds.Tables("Section")
    .DataBindings.Add("SelectedValue", bs, "Section")
End With

cbSection.SelectedIndex = 0