none
Datatable , Data binding, binding source and calculations RRS feed

  • Question

  • Hi,

    I am trying to build a small calculation application ( e.g calculation of area for different objects such as square , circle). I have following code which contains a small data table with 3 columns , 2 columns for parameters and 1 for result and i bound those columns to 3 text boxes (tb_p1,tb_p2, tb_r)  . so the idea is people can choose different objects from a combo box (e.g. Square or circle) and calculate the area or volume and add it as datatable rows.

    Now i dont know how to calculate the result and add it to row. for example if user selects the first row and if they change the parameters the result should also change and update in the datatable and update the controls. 

    I have a following code, it adds a new row to my datatable but it does not show values in bound controls after adding and does not update the results after changing the parameters. Could anyone please show me an example how to make this work?. 

    I cannot use expression column because i will use complex calculations with more parameters in the original application.

    Should all the parameters be declared as properties ? what is the correct approach for this kind of application?.

    thanks

    Public Class Form1 Public BS As New BindingSource() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim DtSet As New DataSet

    With DtSet

    .Tables.Add("Math") With .Tables("Math") .Columns.Add("Param1", GetType(Double)) .Columns.Add("Param2", GetType(Double)) .Columns.Add("Result", GetType(Double)) '.Columns("Result").DefaultValue = DBNull.Value End With

    End With BS.DataSource = DtSet.Tables("Math") DataGridView1.DataSource = BS '//Parameters tb_p1.DataBindings.Add("Text", BS, "Param1") tb_p2.DataBindings.Add("Text", BS, "Param2") '//Calculate result with the function tb_r.DataBindings.Add("Text", BS, "Result") End Sub Function Calculate_FN(ByVal p1 As Double, ByVal p2 As Double) As Double Dim res As Double res = p1 * p2 * 1000 Return res End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim val1, val2 As Double val1 = Me.tb_p1.Text val2 = Me.tb_p2.Text Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView) drv.Row("Param1") = val1 drv.Row("Param2") = val2 drv.Row("Result") = Calculate_FN(val1, val2) drv.Row.EndEdit() End Sub End Class


    Thanks


    • Edited by Shan1986 Tuesday, July 16, 2019 7:32 PM
    Tuesday, July 16, 2019 7:28 PM

All replies

  • Hi

    Here is my understanding of what you want. Based on your code with many changes: res textbox changed to be a 3rd parameter, changed (simplified) the datatable setup, removed the texbox bindings (not needed), added a Function to return a valid Double value from a String (or ZERO if not a valid Double), added a little DayaGridView formatting as an example (particularly, the display of Double values) and maybe other changes I may have forgotten.

    EDIT: Ah yes, added a small Class to handle the results as both a String (type of calculation) and the numeric result.

    Option Strict On Option Explicit On Public Class Form1 Private BS As New BindingSource() Private dt As New DataTable Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("Type", GetType(String)) .Columns.Add("Param1", GetType(Double)) .Columns.Add("Param2", GetType(Double)) .Columns.Add("Param3", GetType(Double)) .Columns.Add("Result", GetType(Double)) End With BS.DataSource = dt ComboBox1.DataSource = {"Circle", "Rectangle", "Triangle"}.ToList With DataGridView1 .DataSource = BS .Columns("Result").DefaultCellStyle.Format = "0.00" .Columns("Result").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight End With End Sub Function Calculate_FN(p1 As Double, p2 As Double, p3 As Double) As Result Dim res As New Result Dim shape As String = ComboBox1.SelectedItem.ToString Select Case shape Case "Circle" ' PI * r ^ 2 With res .Type = "Circle" .res = Math.PI * p1 ^ 2 End With Case "Rectangle" ' width * height With res .Type = "Rectangle" .res = p1 * p2 End With Case "Triangle" ' 1/2 * base * height With res .Type = "Triangle" Dim r As Double = (p1 + p2 + p3) / 2 .res = Math.Sqrt(r * (r - p1) * (r - p2) * (r - p3)) End With End Select Return res End Function Class Result Property Type As String Property res As Double End Class Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click Dim val1, val2, val3 As Double val1 = GetDouble(tb_p1.Text) val2 = GetDouble(tb_p2.Text) val3 = GetDouble(tb_p3.Text) Dim res As Result = Calculate_FN(val1, val2, val3) dt.Rows.Add(res.Type, val1, val2, val3, res.res) End Sub ' Suggest this Function to convert string ' values to Double value Function GetDouble(s As String) As Double Dim v As Double = 0.0 If Double.TryParse(s, v) Then Return v Return 0.0 End Function Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim shape As String = ComboBox1.SelectedItem.ToString Select Case shape Case "Circle" tb_p1.Visible = True tb_p2.Visible = False tb_p3.Visible = False Case "Rectangle" tb_p1.Visible = True tb_p2.Visible = True tb_p3.Visible = False Case Else tb_p1.Visible = True tb_p2.Visible = True tb_p3.Visible = True End Select

            tb_p1.Text = Nothing
            tb_p2.Text = Nothing
            tb_p3.Text = Nothing End Sub End Class



    Regards Les, Livingston, Scotland





    • Edited by leshay Tuesday, July 16, 2019 9:21 PM Added image
    Tuesday, July 16, 2019 8:33 PM
  • Hi,

    fix your code:

    Public Class Form1
    
        Public BS As New BindingSource()
    
        Dim DtSet As New DataSet
        Dim i = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                End With
    
            End With
    
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
    
            '//Parameters
            'tb_p1.DataBindings.Add("Text", BS, "Param1")
            'tb_p2.DataBindings.Add("Text", BS, "Param2")
    
            '//Calculate result with the function
            tb_r.DataBindings.Add("Text", BS, "Result")
        End Sub
    
        Function Calculate_FN(ByVal p1 As Double, ByVal p2 As Double) As Double
    
            Dim res As Double
            res = p1 * p2 * 1000
            Return res
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim val1, val2 As Double
    
            val1 = Me.tb_p1.Text
            val2 = Me.tb_p2.Text
    
            'Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
            'drv.Row("Param1") = val1
            'drv.Row("Param2") = val2
            'drv.Row("Result") = Calculate_FN(val1, val2)
            'drv.Row.EndEdit()
            BS.EndEdit()
            DtSet.Tables("Math").Rows.Add()
            DtSet.Tables("Math").Rows(i).Item("Param1") = val1
            DtSet.Tables("Math").Rows(i).Item("Param2") = val2
            DtSet.Tables("Math").Rows(i).Item("Result") = Calculate_FN(val1, val2)
            BS.Position += 1
            i += 1
        End Sub
    
    End Class
    


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 2:55 AM
    Moderator
  • Hi Shan,
    use BS.EndEdit instead of drv.Rows.EndEdit !

    But you have a problem. Binding the TextBoxes EndEdit saves the values from the TextBoxes to the current row. After this you insert a new row with the saved values.

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim val1, val2 As Double
    
        val1 = CType(Me.tb_p1.Text, Double)
        val2 = CType(Me.tb_p2.Text, Double)
    
        Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
        drv.Row("Param1") = val1
        drv.Row("Param2") = val2
        drv.Row("Result") = Calculate_FN(val1, val2)
        'drv.Row.EndEdit()
        BS.EndEdit()
      End Sub


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Wednesday, July 17, 2019 6:05 AM
  • Hi,

    fix your code:

    Public Class Form1
    
        Public BS As New BindingSource()
    
        Dim DtSet As New DataSet
        Dim i = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                End With
    
            End With
    
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
    
            '//Parameters
            'tb_p1.DataBindings.Add("Text", BS, "Param1")
            'tb_p2.DataBindings.Add("Text", BS, "Param2")
    
            '//Calculate result with the function
            tb_r.DataBindings.Add("Text", BS, "Result")
        End Sub
    
        Function Calculate_FN(ByVal p1 As Double, ByVal p2 As Double) As Double
    
            Dim res As Double
            res = p1 * p2 * 1000
            Return res
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim val1, val2 As Double
    
            val1 = Me.tb_p1.Text
            val2 = Me.tb_p2.Text
    
            'Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
            'drv.Row("Param1") = val1
            'drv.Row("Param2") = val2
            'drv.Row("Result") = Calculate_FN(val1, val2)
            'drv.Row.EndEdit()
            BS.EndEdit()
            DtSet.Tables("Math").Rows.Add()
            DtSet.Tables("Math").Rows(i).Item("Param1") = val1
            DtSet.Tables("Math").Rows(i).Item("Param2") = val2
            DtSet.Tables("Math").Rows(i).Item("Result") = Calculate_FN(val1, val2)
            BS.Position += 1
            i += 1
        End Sub
    
    End Class


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks Alex and Lex, I need it with data binding and also if the user selects the row from the datagridview , it has to show the values back in the bound text boxes and if the user change the text box value then it should calculate again automatically as he type. for e.g user first select the circle and then he comes back and select the triangle so result result should change & calculate automatically. 

    the datagridview will be ready only. so i need databindings. Is it two way binding with property change events? 


    Wednesday, July 17, 2019 7:04 AM
  • Hi Shan,
    you can use a calculated column instead of function Calculate_FN like in this demo:

    Option Strict On
    Public Class Form35
    
      ' simulate designer
      Private DataGridView1 As New DataGridView With {.Dock = DockStyle.Fill}
      Private WithEvents tb_p1 As New TextBox With {.Dock = DockStyle.Top}
      Private WithEvents tb_p2 As New TextBox With {.Dock = DockStyle.Top}
      Private tb_r As New TextBox With {.Dock = DockStyle.Top, .[ReadOnly] = True}
      Private WithEvents Button1 As New Button With {.Dock = DockStyle.Top, .Text = "New Row"}
    
      '
      Public BS As New BindingSource()
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' simulate designer
        Me.Controls.AddRange(New Control() {DataGridView1, Button1, tb_r, tb_p2, tb_p1})
    
        ' 
        Dim DtSet As New DataSet
    
        With DtSet
    
          .Tables.Add("Math")
          With .Tables("Math")
            With .Columns
              .Add("Param1", GetType(Double))
              .Add("Param2", GetType(Double))
              .Add("Result", GetType(Double), "Param1 * Param2 * 1000")
            End With
          End With
        End With
    
        BS.DataSource = DtSet.Tables("Math")
        DataGridView1.DataSource = BS
    
        '//Parameters
        tb_p1.DataBindings.Add("Text", BS, "Param1")
        tb_p2.DataBindings.Add("Text", BS, "Param2")
    
        '//Calculate result with the function
        tb_r.DataBindings.Add("Text", BS, "Result")
    
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
        drv.Row("Param1") = 0
        drv.Row("Param2") = 0
        BS.EndEdit()
      End Sub
    
      Private Sub tb_p1_LostFocus(sender As Object, e As EventArgs) Handles tb_p1.LostFocus, tb_p2.LostFocus
        BS.EndEdit()
      End Sub
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Wednesday, July 17, 2019 7:24 AM
  • Hi,

    like this?

    Public Class Form1
    
        Public BS As New BindingSource()
    
        Dim i = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            Dim DtSet As New DataSet
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                End With
    
            End With
    
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
    
            '//Parameters
    
    
            '//Calculate result with the function
            tb_r.DataBindings.Add("Text", BS, "Result")
        End Sub
    
        Function Calculate_FN(ByVal p1 As Double, ByVal p2 As Double) As Double
    
            Dim res As Double
            res = p1 * p2 * 1000
            Return res
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim val1, val2 As Double
    
            val1 = Me.tb_p1.Text
            val2 = Me.tb_p2.Text
    
            Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
            drv.Row("Param1") = val1
            drv.Row("Param2") = val2
            drv.Row("Result") = Calculate_FN(val1, val2)
            'drv.Row.EndEdit()
            BS.EndEdit()
    
        End Sub
    
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            tb_p1.DataBindings.Clear()
            tb_p2.DataBindings.Clear()
            tb_p1.DataBindings.Add("Text", BS, "Param1")
            tb_p2.DataBindings.Add("Text", BS, "Param2")
            BS.Position = e.RowIndex
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 7:29 AM
    Moderator
  • Hi,

    like this?

    Public Class Form1
    
        Public BS As New BindingSource()
    
        Dim i = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            Dim DtSet As New DataSet
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                End With
    
            End With
    
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
    
            '//Parameters
    
    
            '//Calculate result with the function
            tb_r.DataBindings.Add("Text", BS, "Result")
        End Sub
    
        Function Calculate_FN(ByVal p1 As Double, ByVal p2 As Double) As Double
    
            Dim res As Double
            res = p1 * p2 * 1000
            Return res
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim val1, val2 As Double
    
            val1 = Me.tb_p1.Text
            val2 = Me.tb_p2.Text
    
            Dim drv As DataRowView = DirectCast(BS.AddNew(), DataRowView)
            drv.Row("Param1") = val1
            drv.Row("Param2") = val2
            drv.Row("Result") = Calculate_FN(val1, val2)
            'drv.Row.EndEdit()
            BS.EndEdit()
    
        End Sub
    
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            tb_p1.DataBindings.Clear()
            tb_p2.DataBindings.Clear()
            tb_p1.DataBindings.Add("Text", BS, "Param1")
            tb_p2.DataBindings.Add("Text", BS, "Param2")
            BS.Position = e.RowIndex
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hi Alex, Thanks what i want to achieve is the result should change immediately as you type or change the parameters or object selection change. I know i have to add somthing like this ..

    .DataBindings.Add("Text", BS, "Param1", True, DataSourceUpdateMode.OnPropertyChanged)

    How do i fire a function on change event so that my result get updated/refreshed automatically and immediately.?

    In your example you show the result after clicking a button but i want the result to be shown in the result box as you type and before clicking a button.



    • Edited by Shan1986 Wednesday, July 17, 2019 8:04 AM
    Wednesday, July 17, 2019 8:03 AM
  • Hi,

    like this?

    Public Class Form1
    
        Public BS As New BindingSource()
        Dim DtSet As New DataSet
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AllowUserToAddRows = False
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                    .Rows.Add()
                End With
    
            End With
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
            tb_p1.DataBindings.Add("Text", BS, "Param1", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_p2.DataBindings.Add("Text", BS, "Param2", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_r.DataBindings.Add("Text", BS, "Result", False, DataSourceUpdateMode.OnPropertyChanged)
            'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        End Sub
    
    
        Private Sub tb_p1_TextChanged(sender As Object, e As EventArgs) Handles tb_p1.TextChanged
    
            'DataGridView1.Focus()
            DataGridView1.Rows(0).Cells(0).Selected = True
            DataGridView1.ClearSelection()
            'DataGridView1.EndEdit()
        End Sub
    
        Private Sub tb_p2_TextChanged(sender As Object, e As EventArgs) Handles tb_p2.TextChanged
    
            DataGridView1.Rows(0).Cells(1).Selected = True
            DataGridView1.ClearSelection()
    
        End Sub
    
        Private Sub tb_r_TextChanged(sender As Object, e As EventArgs) Handles tb_r.TextChanged
            DataGridView1.Rows(0).Cells(2).Selected = True
            DataGridView1.ClearSelection()
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 18, 2019 3:16 AM
    Moderator
  • Hi,

    like this?

    Public Class Form1
    
        Public BS As New BindingSource()
        Dim DtSet As New DataSet
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AllowUserToAddRows = False
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                    .Rows.Add()
                End With
    
            End With
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
            tb_p1.DataBindings.Add("Text", BS, "Param1", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_p2.DataBindings.Add("Text", BS, "Param2", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_r.DataBindings.Add("Text", BS, "Result", False, DataSourceUpdateMode.OnPropertyChanged)
            'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        End Sub
    
    
        Private Sub tb_p1_TextChanged(sender As Object, e As EventArgs) Handles tb_p1.TextChanged
    
            'DataGridView1.Focus()
            DataGridView1.Rows(0).Cells(0).Selected = True
            DataGridView1.ClearSelection()
            'DataGridView1.EndEdit()
        End Sub
    
        Private Sub tb_p2_TextChanged(sender As Object, e As EventArgs) Handles tb_p2.TextChanged
    
            DataGridView1.Rows(0).Cells(1).Selected = True
            DataGridView1.ClearSelection()
    
        End Sub
    
        Private Sub tb_r_TextChanged(sender As Object, e As EventArgs) Handles tb_r.TextChanged
            DataGridView1.Rows(0).Cells(2).Selected = True
            DataGridView1.ClearSelection()
        End Sub
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Yes like this, but result should calculate immediately and automatically. any idea?
    Monday, July 22, 2019 3:05 PM
  • Hi,

    add a code:

    Public Class Form1
    
        Public BS As New BindingSource()
        Dim DtSet As New DataSet
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AllowUserToAddRows = False
    
            With DtSet
    
                .Tables.Add("Math")
                With .Tables("Math")
                    .Columns.Add("Param1", GetType(Double))
                    .Columns.Add("Param2", GetType(Double))
                    .Columns.Add("Result", GetType(Double))
                    '.Columns("Result").DefaultValue = DBNull.Value
                    .Rows.Add()
                End With
    
            End With
    
            BS.DataSource = DtSet.Tables("Math")
            DataGridView1.DataSource = BS
            tb_p1.DataBindings.Add("Text", BS, "Param1", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_p2.DataBindings.Add("Text", BS, "Param2", False, DataSourceUpdateMode.OnPropertyChanged)
            tb_r.DataBindings.Add("Text", BS, "Result", False, DataSourceUpdateMode.OnPropertyChanged)
            'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        End Sub
    
    
        Private Sub tb_p1_TextChanged(sender As Object, e As EventArgs) Handles tb_p1.TextChanged
    
            'DataGridView1.Focus()
            DataGridView1.Rows(0).Cells(0).Selected = True
            DataGridView1.ClearSelection()
            'DataGridView1.EndEdit()
        End Sub
    
        Private Sub tb_p2_TextChanged(sender As Object, e As EventArgs) Handles tb_p2.TextChanged
    
            DataGridView1.Rows(0).Cells(1).Selected = True
            DataGridView1.ClearSelection()
            tb_r.Text = (CInt(tb_p1.Text) * CInt(tb_p2.Text) * 1000).ToString()
        End Sub
    
        Private Sub tb_r_TextChanged(sender As Object, e As EventArgs) Handles tb_r.TextChanged
            DataGridView1.Rows(0).Cells(2).Selected = True
            DataGridView1.ClearSelection()
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 23, 2019 1:21 AM
    Moderator