locked
Execute calculation on when importing rows from a file to data table? RRS feed

  • Question

  • Hallo,

    I am making a simple program , which calculates an area for the selected object (e.g square  or rectangle, but actual program calculates a complicated math)

    I have combo box where the objects are listed for selection and appropriate fields are shown/hidden depending on the object, for e.g. if i select square in the combo box , text field length is shown and area is calculated by a separate sub for the square. separate sub for rectangle and circle. calculation is done automatically as i type or change object

    All the variables are stored in a data table (via binding source) as record for each object e.g, selected object , parameters such as length, radius etc, and area. All fields are data bound to data table by binding source.

    Program works in a normal use as expected. Now i save this file as xml and import existing rows from this xml (or already calculated objects) to the program by import row or bindingsource.addnew,

    when i import row it does not calculate the area for the first row (doing by  bindingsource.addnew ) but it calculates from the second row on ..

    How can i make this work for the first row, or which event i should trigger? or how it is done normally?

    idea is i can import the old file and calculate according to new calculation method.

    thanks.

     

    Monday, September 14, 2020 10:55 AM

All replies

  • Seems that the code to calc is not being called. What happens if you move to the second item then back to the first? If the first item then calculates this means you need to manually call the code that performs the calculations and if that code say an event of the ComboBox take that code out of the event, place in a sub and call it from the event and directly after the data has loaded into the ComboBox. If moving second back to first does not work we need to see your code.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 14, 2020 11:09 AM
  • Seems that the code to calc is not being called. What happens if you move to the second item then back to the first? If the first item then calculates this means you need to manually call the code that performs the calculations and if that code say an event of the ComboBox take that code out of the event, place in a sub and call it from the event and directly after the data has loaded into the ComboBox. If moving second back to first does not work we need to see your code.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, for e.g even if i have the same object 10 times with different parameter ..first row calculation is not updating but remaining 9 items get updated. it seems order or object does not matter.
    Monday, September 14, 2020 11:14 AM
  • Did you try what I suggested?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 14, 2020 11:54 AM
  • Hi Shan1986,

    According to your question, we need more information to reproduce your problem.

    Could you provide some related code here? It will help us analzye your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    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, September 15, 2020 1:28 AM
  • ok i found the problem, the problem is when i import the rows, object combo box was not not updated or automatically reflect the imported row object.

    I wrote the code in datagridview cell click event to update the combobox selected item to reflect the object present in a row. 

    this is the part i am still having problem for example binding combo box selected item to data table and vice versa when i select a different row in datagridview. datagrid cell click event works normally but when i import have to execute cell click event how do i do that. Any simple method or concrete method for two way binding of combo box (like text box databinding)

    Thanks

      

     
    Tuesday, September 15, 2020 9:45 AM
  • Hi Shan1986,

    Thanks for your feedback.

    >>datagrid cell click event works normally but when i import have to execute cell click event how do i do that. 

    We need some related code to reproduce your problem and make a test.

    >>Any simple method or concrete method for two way binding of combo box (like text box databinding)

    You can consider using BindingList<T>.

    Here's a reference you may need: How to two-way bind object to combo Box in windows application

    Besides, if I have any misunderstanding, please provide more details or some code here.

    Best Regards,

    Xingyu Zhao


    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, September 16, 2020 2:45 AM
  • Hallo Xindyu,

    I was looking at  BindingList as option but i still can not figure out how i can make it two way. Here is my code. Any idea how i can bind combobox selected value to datatable?. 

    ublic Class Form1
        Public BS As New BindingSource
        Public myList As BindingList(Of Books)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim table As New DataTable("Tab1")
            table.Columns.Add("Val", GetType(Integer))
            table.Columns.Add("Name", GetType(String))
            BS.DataSource = table
            DataGridView2.DataSource = table
    
            myList = New BindingList(Of Books)()
            myList.Add(New Books(1, "Outdoor"))
            myList.Add(New Books(8, "Hardware"))
            myList.Add(New Books(3, "Tools"))
            myList.Add(New Books(6, "Books"))
            myList.Add(New Books(2, "Appliances"))
            DataGridView1.DataSource = myList
    
    
            ComboBox1.DataSource = myList
            ComboBox1.ValueMember = "MyInt"
            ComboBox1.DisplayMember = "MyString"
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ComboBox1.SelectedValue = 1
        End Sub
    
    End Class
    
    Public Class Books
        Implements INotifyPropertyChanged
        Private _myInt As Integer
        Private _myString As String
        Public Sub New(myInt As Integer, myString As String)
            _myInt = myInt
            _myString = myString
        End Sub
        Public Property MyInt() As Integer
            Get
                Return _myInt
            End Get
            Set(value As Integer)
                _myInt = value
                OnPropertyChanged("MyInt")
            End Set
        End Property
        Public Property MyString() As String
            Get
                Return _myString
            End Get
            Set(value As String)
                _myString = value
                OnPropertyChanged("MyString")
            End Set
        End Property
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Protected Overridable Sub OnPropertyChanged(propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class

    Thanks

    Thursday, September 17, 2020 2:17 PM
  • Hi Shan1986,

    Thanks for your feedback.

    If BindingList can help you solve the problem, did you consider putting all the datatable's data into a 'List<Books>' and then create BindingList with the list?

    For example:

        Private Function ConvertToList(ByVal dt As DataTable) As List(Of Books)
            Dim convertedList = (From rw In dt.AsEnumerable() Select New Books() With {
            .MyInt = Convert.ToInt32(rw("Val")),
            .MyString = Convert.ToString(rw("Name"))
        }).ToList()
            Return convertedList
        End Function
    Dim myList As BindingList(Of Books) = New BindingList(Of Books)(ConvertToList(table))

    Best Regards,

    Xingyu Zhao


    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.

    Friday, September 18, 2020 8:09 AM