Results 1 to 5 of 5

Thread: Databinding issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    21

    Databinding issue

    I need display parent and child or Master and detail table in two datagrids. The source table is not fixed. In winform, I added two databindings and used below method. However, there is no databindings in WPF. How can I migrated the previous Winform to WPF? (below code has been modified in WPF except for bindingsource1 and bindingsouce2)

    Code:
            Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Loaded
    
            Me.MasterDGV.ItemsSource = Me.BindingSource1
            Me.DetailDGV.ItemsSource = Me.BindingSource2
    
        End Sub
    
        Private Sub LoadData()
    
            Data = Nothing
            Data = New DataSet
    
            SourcePath = "C:\Quote Kit\Sourcefile\emg_A_US_CN_USD_CIP.xlsx"
            Currency = "USD"
    
            MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + SourcePath + "; Extended Properties=Excel 12.0;")
            MasterAdapter = New System.Data.OleDb.OleDbDataAdapter("select Partnumber,Option,Description," & Currency & " from [Sheet1$] where partnumber=option", MyConnection)
            DetailAdapter = New System.Data.OleDb.OleDbDataAdapter("select Partnumber,Option,Description," & Currency & " from [Sheet1$] where partnumber<>option", MyConnection)
    
            MasterAdapter.Fill(Data, "Parent")
            DetailAdapter.Fill(Data, "Child")
            MyConnection.Close()
    
    
        End Sub
    
        'Define Master and Detail binding source
        Public Sub BindingData()
    
            Try
    
                Dim Data As DataSet = Me.GetDataSet()
    
                Me.bindingsource1.datasource = Data
                Me.BindingSource1.DataMember = "Parent"
    
                Me.BindingSource2.DataSource = Me.BindingSource1
                Me.BindingSource2.DataMember = "ParentChild"
    
                Me.MasterDGV.ItemsSource = Me.BindingSource1
                Me.DetailDGV.ItemsSource = Me.BindingSource2
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        'Load Data
        Public Function GetDataSet() As DataSet
    
            LoadData()
    
            Data.Relations.Add("ParentChild", Data.Tables("Parent").Columns("Partnumber"), Data.Tables("Child").Columns("Partnumber"))
    
            Return Data
    
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Databinding issue

    Have you investigated the MVVM design pattern? If working in WPF, you should. Data-binding is far more advanced in WPF than in WinForms.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    21

    Re: Databinding issue

    Hi jmcilhinney, sorry that I could not ask the question clearly.

    I have two datagrids which need display two datatables with relationship. The datatables are not fixed. I also have a search textbox which can filter two datagrids. I learnt from you last year in winform. But I failed to do that in WPF. In WPF, I managed to create the dataset with two different tables. But because I need use the filter in textbox, so I dont know how to do that.

    Code:
    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Loaded
    
    '--------winform works, WPF doesn't work-----------------
            Me.MasterDGV.ItemsSource = Me.BindingSource1
            Me.DetailDGV.ItemsSource = Me.BindingSource2
    '-------------------------------------------------------
    
    '---------LoadDate() can work if I do not bind anything---------------
            'LoadData() 
    '---------------------------------------------------------------------
    
        End Sub
    
        Private Sub LoadData()
    
            Data = Nothing
            Data = New DataSet
            SourcePath = “C:\emg_A_US_CN_USD_CIP.xlsx"
            Currency = "USD"
    
    
            MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + SourcePath + "; Extended Properties=Excel 12.0;")
            MasterAdapter = New System.Data.OleDb.OleDbDataAdapter("select Partnumber,Option,Description," & Currency & " from [Sheet1$] where partnumber=option", MyConnection)
            DetailAdapter = New System.Data.OleDb.OleDbDataAdapter("select Partnumber,Option,Description," & Currency & " from [Sheet1$] where partnumber<>option", MyConnection)
    
            MasterAdapter.Fill(Data, "Parent")
            DetailAdapter.Fill(Data, "Child")
    
    '---------If use LoadData and below two, the datagrid can display. ---------------
            'MasterDGV.ItemsSource = Data.Tables("Parent").DefaultView  
            'DetailDGV.ItemsSource = Data.Tables("Child").DefaultView    
    '---------------------------------------------------------------------------
            MyConnection.Close()
    
        End Sub
    
        'Define Master and Detail binding source
        Public Sub BindingData()
    
            Try
    
                Dim Data As DataSet = Me.GetDataSet()
    
    '--------winform works, WPF doesn't work-----------------
                Me.BindingSource1.DataSource = Data
                Me.BindingSource1.DataMember = "Parent"
    
                Me.BindingSource2.DataSource = Me.BindingSource1
                Me.BindingSource2.DataMember = "ParentChild"
    
                Me.MasterDGV.ItemsSource = Me.BindingSource1
                Me.DetailDGV.ItemsSource = Me.BindingSource2
    '----------------------------------------------------------
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        'Load Data
        Public Function GetDataSet() As DataSet
    
            LoadData()
    
            Data.Relations.Add("ParentChild", Data.Tables("Parent").Columns("Partnumber"), Data.Tables("Child").Columns("Partnumber"))
    
            Return Data
    
        End Function
    
    'Search by filtering
        Private Sub TextBxSearch_TextChanged(sender As Object, e As EventArgs) Handles TextBxSearch.TextChanged
    
    '--------winform works, WPF doesn't work-----------------
            Me.BindingSource1.Filter = String.Format("Partnumber Like '%{0}%'", Me.TextBxSearch.Text)
    '--------------------------------------------------------
    
        End Sub

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Databinding issue

    Hello,

    For databinding a WPF DataGrid control using DataTable or List<T> object, open the link VB.NET & C#.NET in my signature. It has examples on how to populate a WPF DataGrid control using those objects.

    As for the recommended practice of databinding controls in WPF, this link is a simplified approach with detailed explanations on how to bind a datagrid control with an ObservableCollection object using MVVM pattern.
    WPF Having Trouble with binding a Datagrid control on load

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    21

    Re: Databinding issue

    Quote Originally Posted by KGComputers View Post
    Hello,

    For databinding a WPF DataGrid control using DataTable or List<T> object, open the link VB.NET & C#.NET in my signature. It has examples on how to populate a WPF DataGrid control using those objects.

    As for the recommended practice of databinding controls in WPF, this link is a simplified approach with detailed explanations on how to bind a datagrid control with an ObservableCollection object using MVVM pattern.
    WPF Having Trouble with binding a Datagrid control on load

    - kgc
    I can load the data from data table to datagrid. But my two data tables can change from time to time, and they have parent and child relationship. I also need filter the parent table when I input in a textbox. The parent datagrid and child datagrid need immediately change along with the input in textbox. I did that in Winform. I could not figure out in WPF.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width