I have created a simple form that inserts/updates/deletes a values for Northwind Customers. Everything works fine, except in order to see a results, I have to close it, and reopen again. My form looks like this :

I've searched tens of articles on how to refresh ListBox, but all of those use interface implementing, or using DataSets, and stuff I have never heard of and cannot implement. It's a very simple project, using simple procedures. Is there an easy way to refresh the list of customers without adding many lines of code?
The simple answer is: myListBox.Items.Refresh();
Are you using ObservableCollection and does your model implement INotifyPropertyChanged these two things will automaticly update the ListBox on any change. no need to explicitly refresh the list.
Here is a small example of using ObservableCollection and INotifyPropertyChanged, obviously you will populate your ObservableCollection from your SQL database.
Window:
public partial class MainWindow : Window,  INotifyPropertyChanged
{
    private ObservableCollection<MyModel> _list = new ObservableCollection<MyModel>();
    private MyModel _selectedModel;
    public MainWindow()
    {
        InitializeComponent();
        List.Add(new MyModel { Name = "James", CompanyName = "StackOverflow"});
        List.Add(new MyModel { Name = "Adam", CompanyName = "StackOverflow" });
        List.Add(new MyModel { Name = "Chris", CompanyName = "StackOverflow" });
        List.Add(new MyModel { Name = "Steve", CompanyName = "StackOverflow" });
        List.Add(new MyModel { Name = "Brent", CompanyName = "StackOverflow" });
    }
    public ObservableCollection<MyModel> List 
    {
        get { return _list; }
        set { _list = value; }
    }
    public MyModel SelectedModel
    {
        get { return _selectedModel; }
        set { _selectedModel = value; NotifyPropertyChanged("SelectedModel"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
Xaml
<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid>
        <ListBox ItemsSource="{Binding ElementName=UI, Path=List}" SelectedItem="{Binding ElementName=UI, Path=SelectedModel}" Margin="0,0,200,0" DisplayMemberPath="DisplayMember" SelectedIndex="0" />
        <StackPanel HorizontalAlignment="Left" Height="100" Margin="322,10,0,0" VerticalAlignment="Top" Width="185">
            <TextBlock Text="Name" />
            <TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.Name, UpdateSourceTrigger=PropertyChanged}" />
            <TextBlock Text="Company Name" />
            <TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.CompanyName, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
    </Grid>
</Window>
Model
public class MyModel : INotifyPropertyChanged
{
    private string _name;
    private string _companyName;
    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }
    public string CompanyName
    {
        get { return _companyName; }
        set { _companyName = value; NotifyPropertyChanged("CompanyName"); }
    }
    public string DisplayMember
    {
        get { return string.Format("{0} ({1})", Name, CompanyName); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
            PropertyChanged(this, new PropertyChangedEventArgs("DisplayMember"));
        }
    }
}
In this case any edit to properties will Update your list instantly, also will update when new Items are added/removed.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With