Hello
This is my first post so I’m sorry if I ramble and don’t explain my issue clearly.
I am binding a dataset (dsTest) with a table called “TempResult” to a datagrid (DatagridView1).
The purpose of the datagrid is to display test labels (set elsewhere in the software) and to allow the user to enter results into an empty column titled results.
To achieve this I’ve filled the dataset with the required test labels then added a column called “Result” with a default value of 0.00.
The user can enter numbers into the “Result” column on the grid then click a button “ADD” to update the changes to the datatset,
When I first open the form the datagrid shows the correct information and any changes made to the Result column are updated correctly – all is well.
Private Sub Add_Results()
Dim ResultColumn As DataColumn
Dim Rows As Integer
Dim i As Integer
If con.State = ConnectionState.Closed Then con.Open()
‘ This selects all the test labels for the test equipment that the user has selected
sqlTEST = "SELECT TestInfo.TestInfoKeyID, TestInfo.Label FROM TestInfo WHERE TestInfo.PATTesterKeyID=" & G_SelectedPATTesterKeyID
daTEST = New OleDb.OleDbDataAdapter(sqlTEST, con)
daTEST.Fill(dsTEST, "TempResult")
ResultColumn = New DataColumn("Result")
dsTEST.Tables("TempResult").Columns.Add(ResultColumn)
daTEST.Update(dsTEST, "TempResult")
Rows = dsTEST.Tables("TempResult").Rows.Count
For i = 0 To Rows - 1
dsTEST.Tables("TempResult").Rows(i).Item("Result") = "0.000"
Next
Me.DataGridView1.Columns(0).Visible = False ' hide 1st column
Me.DataGridView1.Columns(1).ReadOnly = True ' results labels can't be edited
Me.DataGridView1.Columns(1).HeaderText = "Test Decription" ' Change header text
Me.DataGridView1.Columns.Item(2).DefaultCellStyle.Format = "n3" ' Format column 5 to 3 decimal places
Me.DataGridView1.Columns.Item(2).ValueType = GetType(Double) ' Inherited format
CType(DataGridView1.Columns(2), DataGridViewTextBoxColumn).MaxInputLength = 7 ' Set max inout length into results column
Me.DataGridView1.RowHeadersVisible = False ' Hide row headers
Me.DataGridView1.AllowUserToAddRows = False ' Removes the blank row at the bottom of the datagrid
Me.DataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill ' Fill the widths of the columns to width of the grid
End Sub
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'------------------------------------------------
' Update DataSet with results entered into grid
'------------------------------------------------
Dim cmdbuilder As New OleDb.OleDbCommandBuilder(daTEST) ' command builder using daTemp
Dim i As Integer
Try
i = daTEST.Update(dsTEST, "TempResult") ' update any changes that have been made in grid
Catch ex As Exception
MsgBox(ex.Message)
End Try
At this point I add the info stored in dsTest to database table.
---------------------------------------------------------------------
dsTEST.Dispose()
daTEST.Dispose()
Me.DataGridView1.DataSource = Nothing ' Set datagrid source to nothing ready for next load
Me.DataGridView1.Update() ' Update datagrid with no data source
Me.Close()
If con.State = ConnectionState.Open Then con.Close()
End Sub
If I close the form and open it up again (i.e. the user wants to add more results) the datagrid shows the results entered the last time the form was use and any changes made to the grid are not updated and the form won’t work again until the software is reloaded.
Any hints will be gratefully received.
Thanks