【问题标题】:Adding item in combobox在组合框中添加项目
【发布时间】:2013-01-27 07:19:49
【问题描述】:

我想向 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

【问题讨论】:

    标签: vb.net winforms combobox datatable


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多