0

I have an ObservableCollection bound to a listbox like this:

string[] selection = comboEmail.GetItemText(comboEmail.SelectedItem).Split(',');

Employee add = new Employee(Convert.ToInt32(selection[0]), selection[1], selection[2], selection[3]);
displayEmp.Add(add);
listEmail.DataSource = displayEmp;

It adds the data to the listbox(listEmail), however, how do I update it after removal. This is what I've tried so far:

int indexRemoval = listEmail.SelectedIndex;
displayEmp.RemoveAt(indexRemoval);
listEmail.DataSource = displayEmp;
//listEmail.Refresh();

but it doesn't work. How would I update the list after a user as clicked the remove button?

rageit
  • 3,513
  • 1
  • 26
  • 38
  • You have some issues with the MVVM way of doing things... Once binded, the ObservableCollection doesn't need to be manually refreshed. Your button should be binded to a Command as well, and the deletion should be using `listEmail.SelectedItem`. Moreover, the binding should take place in the XAML, not the code-behind (which I have no experience with, so I can't help you there). The idea is to let the logic completely out of the UI. – Kilazur Jun 11 '15 at 14:13
  • @Kilazur - Thanks, I stick to using regular List<> collection. – Edward Snowden Jun 11 '15 at 14:18
  • why would you want to do that? – Kilazur Jun 11 '15 at 14:18
  • I have no experience with ObservableCollection, it was suggested to me in another post. I was using List<> collection before, and it was working. I'll stick with it. Any reason, other than more lines of code, why I shouldn't use List<>? – Edward Snowden Jun 11 '15 at 14:21
  • You're coming from Winforms, right? I've been there, it's a pretty complicated step to go to MVVM WPF. If you need something that works quickly, suit yourself and use a List; but eventually, if you want to keep on using WPF, you should learn MVVM, bindings and such. As for the reasons, it's not just about a single list, it's about everything you'll use in your program. If it's short, don't bother with MVVM; if you need something maintenable in the future, it's MVVM. – Kilazur Jun 11 '15 at 14:23
  • @Kilazur - I shall learn it in good time. Right now I'm building a Winform application, and List<> does the job I need it to do. – Edward Snowden Jun 11 '15 at 14:25
  • oh, damn me, I saw ObservableCollection and assumed WPF, my bad. – Kilazur Jun 11 '15 at 14:26
  • @Kilazur - Not an issue :D – Edward Snowden Jun 11 '15 at 14:28

1 Answers1

0

You need to set the ItemSource instead of the DataSource. This assumes that you have also set the DataContext.

listEmail.ItemSource = displayEmp;

Since you have defined your displayEmp as ObservableCollection, your updates should reflect automatically.

EDIT: Here is a sample for your reference using BindingList which auto refreshes the UI:

public BindingList<Data> DataList { get; set; }

public void Load()
{
    DataList = new BindingList<Data>
                   {
                        new Data
                            {
                                Key = "Key1",
                                Value = "Value1"
                            },
                        new Data
                            {
                                Key = "Key2",
                                Value = "Value2"
                            },
                        new Data
                            {
                                Key = "Key3",
                                Value = "Value3"
                            }
                    };
     listBox1.DataSource = DataList;
     listBox1.DisplayMember = "Value";
     listBox1.ValueMember = "Key";
}

public class Data
{
     public string Key { get; set; }
     public string Value { get; set; }
}   

private void button1_Click(object sender, EventArgs e)
{
    DataList.RemoveAt(1);
}
rageit
  • 3,513
  • 1
  • 26
  • 38
  • How do I properly remove an item? Could you please update your code to show how to do the removal? – Edward Snowden Jun 11 '15 at 14:26
  • I'm building a WinForm application, and ItemSource isn't available to me under ListBox. – Edward Snowden Jun 11 '15 at 14:29
  • ItemsSource is WPF only, it seems. [Try with a BindingList](http://stackoverflow.com/questions/17615069/how-to-refresh-datasource-of-a-listbox-in-c-sharp-winforms). (edit: you see how much of a pain Winforms is ;p) – Kilazur Jun 11 '15 at 14:33
  • @Kilazur - Thanks for the suggestions, and link :D – Edward Snowden Jun 11 '15 at 14:35
  • Since you mentioned ObservableCollection I thought you were working with WPF. Like @Kilazur mentioned BindingList works pretty for you. – rageit Jun 11 '15 at 14:42