我有一个 VB.net 脚本,可以将信息放入MSSQL数据库中的表中。
我设法仅使用数据网格视图来更新数据。
现在我正在尝试弄清楚如何更新同一表中引用其他两列之间的计算的另一列。
对于该列(假设Column3
),我使用了计算的列规范属性,如下所示:(列 1*列 2)。
无论如何,当我更新Column1
时,我希望同时看到Column3
的变化。
实际上Column3
的值正在更改,但在我重新启动调试之前它不会显示。
所以这是交易...我想同时看到变化。我应该使用哪种代码?
我是初学者,所以我不知道可能性。
我应该为此使用触发器吗?如果我必须如何?(我尝试Select Column1, Column2, Column3 as (Column1/Coulmn2) from Table1
然后从数据网格视图查看,但 id 也不起作用。我不记得它不起作用的确切原因)
数据库 1,表名称:表 1,列:列 1,列 2,列 3)
法典:
Public Sub tryupdate(Query As String)
Try
sqlcon.Open()
sqlCmd = New SqlCommand(Query, sqlcon)
sqlDA = New SqlDataAdapter(sqlCmd)
Dim SqlCmdBuild As SqlCommandBuilder = New SqlCommandBuilder(sqlDA)
sqlDA.UpdateCommand = SqlCmdBuild.GetUpdateCommand()
sqlDA.Update(sqlDataset)
sqlcon.Close()
Catch ex As Exception
If sqlcon.State = ConnectionState.Open Then sqlcon.Close()
MsgBox(ex.Message)
End Try
End Sub
Private Sub dataset_fill()
sql.tryupdate("select * from [Quantities&Planning]")
DgwNewLayout.DataSource = sql.sqlDataset.Tables(0)
End Sub
Private Sub DgwNewLayout_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgwNewLayout.CellValueChanged
dataset_fill()
End Sub
private Sub filltocell()
Dim cell1 As Integer
Dim cell2 As Integer
Dim cellplus As Integer
iCell1 = DgwNewLayout.CurrentRow.Cells(0).Value
icell2 = DgwNewLayout.CurrentRow.Cells(1).Value
cellplus = cell1 * cell2
DgwNewLayout.CurrentRow.Cells(2).Value = cellplus
结束子
我使用此代码作为解决方案。