1

我又回来了另一个 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

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

任何帮助总是受到赞赏。

提前谢谢各位

4

2 回答 2

5

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

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)

希望这很有用。

于 2013-11-20T22:04:13.367 回答
1

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

代码如下

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

希望这可以帮助其他陷入困境的人

祝你好运

于 2013-09-09T17:06:34.317 回答