Updating calculated columns in a mssql server database by Datagridview using vb.net

nbewdwxp  于 11个月前  发布在  SQL Server
关注(0)|答案(1)|浏览(98)

I have a VB.net script that puts information into a table in MSSQL database.
I managed to update datas by using only datagridview.
Now I am trying to figure out how to update another column in same table that refers a calculation between two other columns.
For that column(let's say Column3 ) I have used computed column specification properity like this: (Column1*Column2).

Anyway when I update Column1 I want to see changes in Column3 at the same time.
Actually value of Column3 is changing but it does not show up until I restart debugging.
So here is the deal... I want to see the changes simultaneously. What kind of code I should use?
I am a beginner so I am not aware of possibilities.
Should I use a trigger for this? If I have to how? (I have tried Select Column1, Column2, Column3 as (Column1/Coulmn2) from Table1 and then viewing from datagridview but id did not work either. I don't remember exact reason why it did not work)

(Database name: Database1, Table name: Table1, Columns: Column1, Column2 ,Column3)

Code:

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
watbbzwu

watbbzwu1#

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 

End Sub

I used this code as solution.

相关问题