vb.net 在组合框中添加项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14824859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:17:38  来源:igfitidea点击:

Adding item in combobox

vb.netwinformscomboboxdatatable

提问by nettoon493

I want to add a item to comboboxwhich already in bounded with some data.

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

Code:

代码:

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

But I want add item, like "---All---", so this is should be the output.

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

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

回答by spajce

Here's the simple solution

这是简单的解决方案

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