none
WPF - How to Refresh() ListView and/or ComboBox binding to a DB? RRS feed

  • Question

  • Hi. I have a database in SQLite (I think it would be the same for any DB though). I have a DataSet created for my database, along with CollectionViewSource's that create automatically when you drag/drop the Data Sources from the Data Sources window.

     

    <CollectionViewSource x:Key="view_NombresViewSource" Source="{Binding Path=view_Nombres, Source={StaticResource rentacarDataSet}}" />
    <CollectionViewSource x:Key="view_ClientesViewSource" Source="{Binding Path=view_Clientes, Source={StaticResource rentacarDataSet}}" />
    <CollectionViewSource x:Key="view_CompañíasViewSource" Source="{Binding Path=view_Compañías, Source={StaticResource rentacarDataSet}}" />
    <CollectionViewSource x:Key="view_AutosViewSource" Source="{Binding Path=view_Autos, Source={StaticResource rentacarDataSet}}" />
    

     

    I have a combobox and a ListView bounded to this datasources:

     

    <ComboBox AllowDrop="False" DataContext="{Binding Source={StaticResource view_CompañíasViewSource}, UpdateSourceTrigger=Explicit}" DisplayMemberPath="Nombre" HorizontalAlignment="Right" IsEditable="True" IsReadOnly="False" IsSynchronizedWithCurrentItem="True" IsTabStop="True" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Margin="0,37.96,290,0" Name="cmbCompañías" SelectedValuePath="ID" Style="{StaticResource ComboBoxNormal}" TabIndex="0" VerticalAlignment="Top" Width="336.5" />
    

     

    The thing is, when I add a record to a table, I'm not getting any way to manually update the combobox. I don't need it to update in real time, but explicitly when I tell it to. Something like myComboBox.ItemsSource.Refresh();, which obviously doesn't work.

    When I set the CollectionViewSource.Source property to an updated dataTable, the ComboBox does reloads, but it takes some time to fill and the whole thing freezes for a few seconds. So, I think what I need (more than updating the ComboBox.ItemsSource) is to update the DataSet itself, but it is a Class! I can't update it...

    How do I update the dataSet/dataTable so it can reflect on the CollectionViewSource?

    yes, I'm this lost and newbie



    • Edited by javoRosas Monday, September 5, 2011 5:42 AM
    Monday, September 5, 2011 5:39 AM

Answers

  • Yes. Right now I'm doing this and it's kinda working:

     

    rentacarDataSetTableAdapters.view_CompañíasTableAdapter view_adapter = new rentacarDataSetTableAdapters.view_CompañíasTableAdapter();
    rentacarDataSet.view_CompañíasDataTable view_table = view_adapter.GetData();
    view_adapter.Update(view_table);
    
    System.Windows.Data.CollectionViewSource view_CompañíasViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("view_CompañíasViewSource")));
                
    view_CompañíasViewSource.Source = view_table;
    


     

    but for some reason I don't think it's a good practice. Besides, it takes the combobox a lot to reload.
    • Marked as answer by javoRosas Saturday, September 10, 2011 10:14 AM
    Monday, September 5, 2011 8:20 PM
  • Ok, I just discovered that the CollectionViewSource updates just fine and quick with this code (though I still think there must be a better way). The ListView updates almost instantly, and so the combobox, BUT the problem here is that the combobox takes almost 10 seconds to actually update the dropdown content.

    My combobox is editable, so I can type to look for an item. When I update the collectionViewSource I can type and the control does suggest the new record in a flash, but if I click the dropDown button it freezes for around 10 seconds before showing the new popup list for the first time since I updated.

    • Marked as answer by javoRosas Saturday, September 10, 2011 10:14 AM
    Monday, September 5, 2011 8:21 PM

All replies

  • Hey JavoRosas, I hope you're fine.

    I believe you may follow the following approach:

    1- You need to create a class for every table you bind to its data in your app.

    2- Create properties that corresponds to the table fields.

    3- Create an ObservableCollection<YourClass> and fill it with the data from DB.

    4- Bind your ComboBox and ListView to the ObservableCollection.

    5- Everytime you update the Collection the control which is bound to it will update its items accordingly.

    Please, mark my reply as "Answer" if it helps or vote it up if it gives a key to the solution.

    Thanks in advance and good luck.


    Ahmed M. Gamil
    "It is not titles that honor men, but men that honor titles."
    • Proposed as answer by Applekoz Saturday, September 10, 2011 9:34 AM
    Monday, September 5, 2011 7:17 AM
  • Just to be sure, you want to be able to manualy pull data from the SQL and update it to your CollectionViewSource ?
    And could you possibly show the codebehind as well ?

    Hey Gamil ! Long time no see.. ;P


    Developing is part of being a developer.
    Monday, September 5, 2011 7:31 AM
  • Hey SharpAspirant, Where have you been, man ?!! .. I hope everything is OK. :)
    Ahmed M. Gamil
    "It is not titles that honor men, but men that honor titles."
    Monday, September 5, 2011 7:53 AM
  • Hi Gamil. Thanks for answering, now I think I'm getting closer to the solution.

    1. Do I really need to create a class for every table? My dataSet contains those classes already, they're named tableNameRow and they contain every field as properties. Should I fill the ObservableCollection with this class instead of a new one?

    2. How do I bind my controls to the ObservableCollection? I declared the collection globally (is this ok?) and it doesn't show up in the sources list, in the Binding menu.

     

    Thanks

    Monday, September 5, 2011 8:19 PM
  • Yes. Right now I'm doing this and it's kinda working:

     

    rentacarDataSetTableAdapters.view_CompañíasTableAdapter view_adapter = new rentacarDataSetTableAdapters.view_CompañíasTableAdapter();
    rentacarDataSet.view_CompañíasDataTable view_table = view_adapter.GetData();
    view_adapter.Update(view_table);
    
    System.Windows.Data.CollectionViewSource view_CompañíasViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("view_CompañíasViewSource")));
                
    view_CompañíasViewSource.Source = view_table;
    


     

    but for some reason I don't think it's a good practice. Besides, it takes the combobox a lot to reload.
    • Marked as answer by javoRosas Saturday, September 10, 2011 10:14 AM
    Monday, September 5, 2011 8:20 PM
  • Ok, I just discovered that the CollectionViewSource updates just fine and quick with this code (though I still think there must be a better way). The ListView updates almost instantly, and so the combobox, BUT the problem here is that the combobox takes almost 10 seconds to actually update the dropdown content.

    My combobox is editable, so I can type to look for an item. When I update the collectionViewSource I can type and the control does suggest the new record in a flash, but if I click the dropDown button it freezes for around 10 seconds before showing the new popup list for the first time since I updated.

    • Marked as answer by javoRosas Saturday, September 10, 2011 10:14 AM
    Monday, September 5, 2011 8:21 PM
  • Here's a link that shows how to bind ObservableCollections:

    http://stackoverflow.com/questions/3913821/wpf-binding-large-amount-of-data-in-listbox It's the same way of binding for the ListView and ComboBox as well.

    Please, mark my reply as "Answer" if it helps or vote it up if it gives a key to the solution.

    Thanks in advance and good luck.

     


    Ahmed M. Gamil
    "It is not titles that honor men, but men that honor titles."
    Wednesday, September 7, 2011 2:39 PM
  • First congrats to so far you have reached. So do you mean that it works well except for click combobox drop-down when updating its content? If that is the case I think this is another issue. and it may due to the UI rendering. Please check this thread http://social.msdn.microsoft.com/Forums/en/wpf/thread/cdac5760-f03e-4e62-aa55-aa312a50639e
    Time is short
    Saturday, September 10, 2011 9:47 AM
  • Hi JavoRosas, I am facing the same issue as u were facing. Could you please help me out in understating how you managed to fix the issue. I am using dataset, so do I need to create new class for every table? If yes, then can you please provide some examples.
    Monday, July 28, 2014 6:13 AM