Hey
I'm creating a program for my work to write CMR's.
What I'm trying to do now is create a collection which automaticly updates my listbox or datagridview when data changes and so far it works all good but I encountered a litle problem.
I created a new class with a IBindingList inhired and added all properties and adjusted it for my purpose.
Only problem is when I add the collection to the listbox. my collection isn't shown.
I have this code right now.
I used an ArrayList because its easier to sort the data.
Everytime a new company is added its sorted automaticly and all this works perfect. but when I do this
Nothing is shown in my listbox.
but as soon I add other data to this listbox after I added it to datasource. then all is shown.
How can I make it like this my data is added to my listbox with the code above.
I'm creating a program for my work to write CMR's.
What I'm trying to do now is create a collection which automaticly updates my listbox or datagridview when data changes and so far it works all good but I encountered a litle problem.
I created a new class with a IBindingList inhired and added all properties and adjusted it for my purpose.
Only problem is when I add the collection to the listbox. my collection isn't shown.
I have this code right now.
Code:
public class CompanyCollection : IBindingList
{
private ArrayList _list = new ArrayList();
private PropertyDescriptor _sortingProperty = TypeDescriptor.GetProperties(typeof(Company)).Find("Name", false);
private ListSortDirection _sortingDirection = ListSortDirection.Ascending;
private CaseInsensitivePropertyComparer _comparer = null;
private ListChangedEventHandler onListChanged = null;
public CompanyCollection()
{
_comparer = new CaseInsensitivePropertyComparer(_sortingProperty, _sortingDirection);
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
public Company this[int index]
{
get { return _list[index] as Company; }
set
{
Company oldCompany = _list[index] as Company;
int i = index;
_list[index] = value;
_list.Sort(_comparer);
i = _list.IndexOf(value);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, i));
}
}
public int Add(Company company)
{
int i = _list.Add(company);
_list.Sort(_comparer);
i = _list.IndexOf(company);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, i));
return i;
}
public Company AddNew()
{
Company company = new Company(0, string.Empty);
_list.Add(company);
return company;
}
public int IndexOf(Company company)
{
return _list.IndexOf(company);
}
public bool Contains(Company company)
{
return _list.Contains(company);
}
public void Remove(Company company)
{
int i = _list.IndexOf(company);
_list.Remove(company);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, i));
}
public void RemoveAt(int index)
{
_list.RemoveAt(index);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
}
public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
public bool IsSynchronized
{
get { return _list.IsSynchronized; }
}
public bool IsFixedSize
{
get { return _list.IsFixedSize; }
}
public bool IsReadOnly
{
get { return _list.IsReadOnly; }
}
public object SyncRoot
{
get { return _list.SyncRoot; }
}
public int Count
{
get { return _list.Count; }
}
public void Clear()
{
_list.Clear();
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
public event ListChangedEventHandler ListChanged
{
add { onListChanged += value; }
remove { onListChanged -= value; }
}
protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (onListChanged != null)
onListChanged(this, ev);
}
public PropertyDescriptor SortProperty
{
get { return _sortingProperty; }
}
public ListSortDirection SortDirection
{
get { return _sortingDirection; }
}
public bool SupportsSorting
{
get { return true; }
}
public bool SupportsSearching
{
get { return true; }
}
public bool IsSorted
{
get { return true; }
}
public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
{
if (property == null)
return;
_sortingProperty = property;
_sortingDirection = direction;
_comparer = new CaseInsensitivePropertyComparer(property, direction);
_list.Sort(_comparer);
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
public int Find(PropertyDescriptor property, object key)
{
PropertyInfo propInfo = typeof(Company).GetProperty(property.Name);
if (key != null)
{
foreach (Company company in _list)
{
if (propInfo.GetValue(company, null).Equals(key))
return _list.IndexOf(company);
}
}
return -1;
}
bool IBindingList.AllowEdit { get { return true; } }
bool IBindingList.AllowNew { get { return true; } }
bool IBindingList.AllowRemove { get { return true; } }
bool IBindingList.SupportsChangeNotification { get { return true; } }
object IBindingList.AddNew() { return null; }
// Unsupported Methods.
void IBindingList.AddIndex(PropertyDescriptor property) { throw new NotSupportedException(); }
void IBindingList.RemoveIndex(PropertyDescriptor property) { throw new NotSupportedException(); }
void IBindingList.RemoveSort() { throw new NotSupportedException(); }
void ICollection.CopyTo(Array array, int index) { }
object IList.this[int index] { get { return null; } set { } }
void IList.Remove(object value) { }
void IList.Insert(int index, object value) { }
int IList.IndexOf(object value) { throw new NotSupportedException(); }
bool IList.Contains(object value) { throw new NotSupportedException(); }
int IList.Add(object value) { throw new NotSupportedException(); }
}
I used an ArrayList because its easier to sort the data.
Everytime a new company is added its sorted automaticly and all this works perfect. but when I do this
Code:
CompanyCollection test = new CompanyCollection();
test.Add(new Company(0, "test 1"));
test.Add(new Company(1, "test 2"));
test.Add(new Company(2, "test 3"));
lbCompanies.DataSource = test;
Nothing is shown in my listbox.
but as soon I add other data to this listbox after I added it to datasource. then all is shown.
How can I make it like this my data is added to my listbox with the code above.
Laatst bewerkt: