Im back again with another DataGidView question
I am creating an application that allows the user to create a ticket for products purchased
This form consists of a DataGidViewwhich allows the user to record the product details along with price and quantity information.
Currently the DataGidView is data bound and data is saved using a data adaptor and command builder at runtime
The columns are created automatically and then i set properties such as formatting options as follows
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
One of the columns is for the stock reference, at the minute i am able to free type this field, but ultimately i would like this to be a combobox column where the user can select from a list of stock references from another table.
I have researched this issue and have found a number of examples which relate to creating a column at runtime but i dont feel that this would work in this situation as if i created the column manually then this would not be bound to the datasource which i want to save the information to.
MANUAL COLUMN CREATION
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
I originally planned on manually adding the items to the combobox but i have seen examples of being able to databind the items. I suppose this is another issue for another time
Any help is always appreciated.
Thanks in advance guys
I have just done something similar that I feel is more flexible, which was inspired by this so I thought that I would share. The main advantage is that you don't need to switch off the auto generation of the columns. This is after I have already set the datagridviews (dgvPickList) datasource to a datatable and also loaded my list of locations in the datatable dtLocations. It replaces the second column, which was bound to the field "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)
Hope that's useful.
I have resolved the issue by disabling the autogenerate columns with the datagridview and manually adding each column. This has allowed me to create a combobox column and add choices to this. Further improvements to this will be to add a datasource to the combobox column so that it automatically loads the items from the stock table
the code is as follows
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
hope this helps anyone else that gets stuck
Good Luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.