数据表附加

时间:2012-12-05 09:40:50

标签: vb.net combobox datatable datarow

请在下面查看我的编码 -

    Dim dtProductName As New DataTable()
    dtProductName.Columns.Add(VP_Product)
    'add new row
    Dim dr As DataRow
    dr = dtProductName.NewRow()
    dr.Item(0) = "All"
    dtProductName.Rows.Add(dr)

    dr = dtProductName.NewRow()
    dr.Item(0) = "None"
    dtProductName.Rows.Add(dr)

    Dim dt As New DataTable()

    dt = cReport.getAllProduct

    For Each drp As DataRow In dt.Rows
        dr = dtProductName.NewRow()
        dr.Item(0) = drp.Item(0)
        dtProductName.Rows.Add(dr)
    Next

    cboProductLine.DataSource = dtProductName
    cboProductLine.ValueMember = VP_Product
    cboProductLine.DisplayMember = VP_Product

“全部”和“无”是附加数据。我想在数据库的另一个数据表中添加这两个。有没有比我的编码更好的方法? (没有循环添加行)谢谢。

1 个答案:

答案 0 :(得分:0)

您无需创建2个数据表。只需使用

修改您从cReport.getAllProduct方法获得的那个

Public Sub InsertAt(row as System.Data.Datarow, pos as Integer)

添加额外“全部”和“无”值的方法。

    Dim dt As New DataTable()

    dt = cReport.getAllProduct

    Dim dr As DataRow

    dr = dt.NewRow()
    dr.Item(0) = "All"
    dt.Rows.InsertAt(dr, 0)

    dr = dt.NewRow()
    dr.Item(0) = "None"
    dt.Rows.InsertAt(dr, 1)

    cboProductLine.DataSource = dt
    'make sure your dt datatable contains the VP_Product column
    cboProductLine.ValueMember = VP_Product
    cboProductLine.DisplayMember = VP_Product