Datatable , Data binding, binding source and calculations

  • Thread starter Thread starter Shan1986
  • Start date Start date
S

Shan1986

Guest
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

Continue reading...
 
Back
Top