我又回来了另一个 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
我最初计划手动将项目添加到组合框中,但我已经看到了能够对项目进行数据绑定的示例。我想这是另一个问题
任何帮助总是受到赞赏。
提前谢谢各位