数据绑定DataGridView中的ComboBox列

时间:2013-09-07 20:47:09

标签: vb.net data-binding datagridview

我又回来了另一个DataGidView问题

我正在创建一个允许用户为购买的产品创建票证的应用程序

此表单由DataGidView组成,允许用户记录产品详细信息以及价格和数量信息。

目前,DataGidView是数据绑定的,并且在运行时使用数据适配器和命令构建器保存数据

自动创建列,然后设置格式选项等属性,如下所示

Private Sub SetTicketList()

    Try
        Con.ConnectionString = CropTrackMod.strConn
        SQLAdaptor.SelectCommand = New SqlClient.SqlCommand("SELECT ID, StockRef, Weight, EstimatedPrice, DespatchedQuantity,EstimatedTransportPer,VATRate, EstimatedTransportTotal,EstimatedVAT, EstimatedLineTotal,TicketRef FROM TicketDetail where ticketref ='x'", Con)
        builder = New SqlClient.SqlCommandBuilder(SQLAdaptor)
        Con.Open()

        Dim myTable As DataTable = New DataTable
        SQLAdaptor.Fill(myTable)

        dgvTicketDetail.DataSource = myTable

        'ID Column
        dgvTicketDetail.Columns(0).Visible = False

        'StockRef
        dgvTicketDetail.Columns(1).HeaderText = "StockRef"
        dgvTicketDetail.Columns(1).CellType.

        'Weight
        dgvTicketDetail.Columns(2).HeaderText = "Weight"
        dgvTicketDetail.Columns(2).DefaultCellStyle.Format = "0"
        dgvTicketDetail.Columns(2).DefaultCellStyle.NullValue = "0"

        'Price Per Unit
        dgvTicketDetail.Columns(3).HeaderText = "Price"
        dgvTicketDetail.Columns(3).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(3).DefaultCellStyle.NullValue = "0.00"

        'Quantity
        dgvTicketDetail.Columns(4).HeaderText = "Quantity"
        dgvTicketDetail.Columns(4).DefaultCellStyle.Format = "0"
        dgvTicketDetail.Columns(4).DefaultCellStyle.NullValue = "0"

        'Transport Cost Per Unit
        dgvTicketDetail.Columns(5).HeaderText = "TransportCostPer"
        dgvTicketDetail.Columns(5).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(5).DefaultCellStyle.NullValue = "0.00"

        'VAT Rate
        dgvTicketDetail.Columns(6).HeaderText = "VAT Rate"
        dgvTicketDetail.Columns(6).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(6).DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns(6).ReadOnly = True

        'Transport Total
        dgvTicketDetail.Columns(7).HeaderText = "Transport Total"
        dgvTicketDetail.Columns(7).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(7).DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns(7).ReadOnly = True

        'VAT Total
        dgvTicketDetail.Columns(8).HeaderText = "VAT Total"
        dgvTicketDetail.Columns(8).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(8).DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns(8).ReadOnly = True

        'line Total
        dgvTicketDetail.Columns(9).HeaderText = "Total"
        dgvTicketDetail.Columns(9).DefaultCellStyle.Format = "0.00"
        dgvTicketDetail.Columns(9).DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns(9).ReadOnly = True


        'line Total
        dgvTicketDetail.Columns(10).HeaderText = "TicketRef"
        dgvTicketDetail.Columns(10).Visible = False
    Finally
        If Con.State = ConnectionState.Open Then
            Con.Close()
        End If
    End Try

End Sub

其中一列用于库存参考,在我能够自由输入此字段的那一刻,但最终我希望这是一个组合框列,用户可以从另一个表的库存参考列表中进行选择

我已经研究过这个问题,并且已经发现了许多与在运行时创建列有关的示例,但我不觉得这会在这种情况下工作,好像我手动创建了列然后这不会绑定到数据源我希望将信息保存到。

手工栏目创作

Dim dgvc As DataGridViewComboBoxCell
dgvc = DataGridView1.Rows(0).Cells(2)

if DataGridView1.Rows(0).Cells(1).Value = "Jack" then
    dgvc.Items.Add("Fe")
    dgvc.Items.Add("Fi")
elseif DataGridView1.Rows(0).Cells(3).Value = "Giant" then
    dgvc.Items.Add("Fo")
    dgvc.Items.Add("Fum")
End if

我最初计划手动将项目添加到组合框中,但我已经看到了能够对项目进行数据绑定的示例。我想这是另一个问题

任何帮助总是受到赞赏。

先谢谢你们

2 个答案:

答案 0 :(得分:5)

我刚刚做了类似的事情,我觉得它更灵活,受到了启发,所以我想我会分享。主要优点是您不需要关闭列的自动生成。这是在我已经将datagridviews(dgvPickList)数据源设置为数据表并且还在数据表dtLocations中加载我的位置列表之后。它取代了第二列,它绑定到字段" Location"

Dim comboBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
comboBoxColumn.HeaderText = "Location"
comboBoxColumn.DataPropertyName = "Location"
comboBoxColumn.DataSource = dtLocations
comboBoxColumn.ValueMember = dtLocations.Columns(0).ColumnName
comboBoxColumn.DisplayMember = dtLocations.Columns(1).ColumnName

dgvPickList.Columns.RemoveAt(1)
dgvPickList.Columns.Insert(1, comboBoxColumn)

希望这很有用。

答案 1 :(得分:1)

我已通过使用datagridview禁用autogenerate列并手动添加每列来解决此问题。这使我能够创建一个组合框列并为此添加选项。对此的进一步改进是将数据源添加到组合框列,以便自动加载库存表中的项目

代码如下

Private Sub SetTicketListTemp()

    Try
        Con.ConnectionString = CropTrackMod.strConn
        SQLAdaptor.SelectCommand = New SqlClient.SqlCommand("SELECT ID, StockRef, Weight, EstimatedPrice, DespatchedQuantity,EstimatedTransportPer,VATRate, EstimatedTransportTotal,EstimatedVAT, EstimatedLineTotal,TicketRef FROM TicketDetail where ticketref ='x'", Con)
        builder = New SqlClient.SqlCommandBuilder(SQLAdaptor)
        Con.Open()

        Dim myTable As DataTable = New DataTable
        SQLAdaptor.Fill(myTable)

        dgvTicketDetail.AutoGenerateColumns = False
        dgvTicketDetail.DataSource = myTable

        'ID Column
        Dim col1 As New DataGridViewTextBoxColumn
        col1.DataPropertyName = "ID"
        col1.HeaderText = "ID"
        col1.Name = "ID"
        col1.Visible = False
        dgvTicketDetail.Columns.Add(col1)


        'StockRef
        Dim col2 As New DataGridViewComboBoxColumn
        col2.DataPropertyName = "StockRef"
        col2.HeaderText = "StockRef"
        col2.Name = "StockRef"

        col2.Items.Add("StockItem1")
        col2.Items.Add("StockItem2")

        dgvTicketDetail.Columns.Add(col2)


        'Weight
        Dim col3 As New DataGridViewTextBoxColumn
        col3.DataPropertyName = "Weight"
        col3.HeaderText = "Weight"
        col3.Name = "Weight"
        col3.DefaultCellStyle.Format = "0"
        col3.DefaultCellStyle.NullValue = "0"
        dgvTicketDetail.Columns.Add(col3)

        'Price Per Unit
        Dim col4 As New DataGridViewTextBoxColumn
        col4.DataPropertyName = "EstimatedPrice"
        col4.HeaderText = "Price"
        col4.Name = "Price"
        col4.DefaultCellStyle.Format = "0.00"
        col4.DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns.Add(col4)

        'Quantity
        Dim col5 As New DataGridViewTextBoxColumn
        col5.DataPropertyName = "DespatchedQuantity"
        col5.HeaderText = "Quantity"
        col5.Name = "Quantity"
        col5.DefaultCellStyle.Format = "0"
        col5.DefaultCellStyle.NullValue = "0"
        dgvTicketDetail.Columns.Add(col5)

        'Transport Cost Per Unit
        Dim col6 As New DataGridViewTextBoxColumn
        col6.DataPropertyName = "EstimatedTransportPer"
        col6.HeaderText = "TransportCostPer"
        col6.Name = "TransportCostPer"
        col6.DefaultCellStyle.Format = "0.00"
        col6.DefaultCellStyle.NullValue = "0.00"
        dgvTicketDetail.Columns.Add(col6)


        'VAT Rate
        Dim col7 As New DataGridViewTextBoxColumn
        col7.DataPropertyName = "VATRate"
        col7.HeaderText = "VAT Rate"
        col7.Name = "VATRate"
        col7.DefaultCellStyle.Format = "0.00"
        col7.DefaultCellStyle.NullValue = "0.00"
        col7.ReadOnly = True
        dgvTicketDetail.Columns.Add(col7)

        'Transport Total
        Dim col8 As New DataGridViewTextBoxColumn
        col8.DataPropertyName = "EstimatedTransportTotal"
        col8.HeaderText = "Transport Total"
        col8.Name = "TransportTotal"
        col8.DefaultCellStyle.Format = "0.00"
        col8.DefaultCellStyle.NullValue = "0.00"
        col8.ReadOnly = True
        dgvTicketDetail.Columns.Add(col8)

        'VAT Total
        Dim col9 As New DataGridViewTextBoxColumn
        col9.DataPropertyName = "EstimatedVAT"
        col9.HeaderText = "VAT Total"
        col9.Name = "VATotal"
        col9.DefaultCellStyle.Format = "0.00"
        col9.DefaultCellStyle.NullValue = "0.00"
        col9.ReadOnly = True
        dgvTicketDetail.Columns.Add(col9)

        'line Total
        Dim col10 As New DataGridViewTextBoxColumn
        col10.DataPropertyName = "EstimatedLineTotal"
        col10.HeaderText = "Total"
        col10.Name = "Total"
        col10.DefaultCellStyle.Format = "0.00"
        col10.DefaultCellStyle.NullValue = "0.00"
        col10.ReadOnly = True
        dgvTicketDetail.Columns.Add(col10)



        'TicketRef
        Dim col11 As New DataGridViewTextBoxColumn
        col11.DataPropertyName = "TicketRef"
        col11.HeaderText = "TicketRef"
        col11.Name = "TicketRef"
        col11.Visible = False
        dgvTicketDetail.Columns.Add(col11)

    Finally
        If Con.State = ConnectionState.Open Then
            Con.Close()
        End If
    End Try

End Sub

希望这有助于其他任何陷入困境的人

祝你好运