Reputation: 1383
I want to bind my object taskCollection
to my checkListBox
. taskCollection
implements the interface IList<Task>
.
When I try to use it as a data source I get the error
System.ArgumentException : Complex DataBinding accepts as a data source either an IList or an IListSource.
Here is my TaskCollection
:
using System;
using System.Collections.Generic;
using System.IO;
namespace CLBatchGUI
{
/// <summary>
/// Description of TaskCollection.
/// </summary>
public class TaskCollection:IList<Task>
{
private List<Task> _tasks;
public TaskCollection()
{
_tasks = new List<Task>();
}
public void executeTasks(){
}
public static void loadFromCSV(string path){
}
public void saveToCSV(string path){
StreamWriter sw= new StreamWriter(path);
saveToCSV(sw);
}
public void saveToCSV(TextWriter sw){
if(_tasks.Count==0)
throw new ApplicationException("There are no tasks to save");
foreach (var Task in _tasks) {
Task.validate();
}
try{
sw.Write("TaskName,");
for(int i=0;i<_tasks[0].job.parameters.Count;i++){
sw.Write(_tasks[0].job.parameters[i].Name);
if(i==_tasks.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
for(int i=0;i<_tasks.Count;i++){
sw.Write(_tasks[i].Name +",");
for(int n=0;n<_tasks[i].job.parameters.Count;n++){
sw.Write(_tasks[i].job.parameters[n].Text);
if(n==_tasks[i].job.parameters.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
}
}catch(Exception ex){
throw new ApplicationException(ex.Message);
}
}
public Task this[int index]{
get{
return _tasks[index];
}
set{
_tasks[index]=value;
}
}
public int Count {
get {
return _tasks.Count;
}
}
public bool IsReadOnly {
get {
return false;
}
}
public int IndexOf(Task item)
{
return _tasks.IndexOf(item);
}
public void Insert(int index, Task item)
{
_tasks.Insert(index,item);
}
public void RemoveAt(int index)
{
_tasks.RemoveAt(index);
}
public void Add(Task item)
{
_tasks.Add(item);
}
public void Clear()
{
_tasks.Clear();
}
public bool Contains(Task item)
{
return _tasks.Contains(item);
}
public void CopyTo(Task[] array, int arrayIndex)
{
_tasks.CopyTo(array,arrayIndex);
}
public bool Remove(Task item)
{
return _tasks.Remove(item);
}
public IEnumerator<Task> GetEnumerator()
{
return _tasks.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _tasks.GetEnumerator();
}
}
}
Updated class:
using System;
using System.Collections.Generic;
using System.IO;
namespace CLBatchGUI
{
/// <summary>
/// Description of TaskCollection.
/// </summary>
public class TaskCollection:IList<Task>,IList
{
private List<Task> _tasks;
public TaskCollection()
{
_tasks= new List<Task>();
}
public void executeTasks(){
}
public static void loadFromCSV(string path){
}
public void saveToCSV(string path){
StreamWriter sw= new StreamWriter(path);
saveToCSV(sw);
}
public void saveToCSV(TextWriter sw){
if(_tasks.Count==0)
throw new ApplicationException("There are no tasks to save");
foreach (var Task in _tasks) {
Task.validate();
}
try{
sw.Write("TaskName,");
for(int i=0;i<_tasks[0].job.parameters.Count;i++){
sw.Write(_tasks[0].job.parameters[i].Name);
if(i==_tasks.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
for(int i=0;i<_tasks.Count;i++){
sw.Write(_tasks[i].Name +",");
for(int n=0;n<_tasks[i].job.parameters.Count;n++){
sw.Write(_tasks[i].job.parameters[n].Text);
if(n==_tasks[i].job.parameters.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
}
}catch(Exception ex){
throw new ApplicationException(ex.Message);
}
}
public object this[int index]{
get{
return _tasks[index];
}
set{
_tasks[index]=value;
}
}
int IList.Count {
get {
return _tasks.Count;
}
}
bool IList.IsReadOnly {
get {
return false;
}
}
int IList.IndexOf(object item)
{
return _tasks.IndexOf(item);
}
void IList.Insert(int index, object item)
{
_tasks.Insert(index,item);
}
void IList.RemoveAt(int index)
{
_tasks.RemoveAt(index);
}
void IList.Add(object item)
{
_tasks.Add(item);
}
void IList.Clear()
{
_tasks.Clear();
}
bool IList.Contains(object item)
{
return _tasks.Contains(item);
}
void IList.CopyTo(object[] array, int arrayIndex)
{
_tasks.CopyTo(array,arrayIndex);
}
bool IList.Remove(object item)
{
return _tasks.Remove(item);
}
// IList.IEnumerator<object> GetEnumerator()
// {
// return _tasks.GetEnumerator();
// }
// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
// {
// return _tasks.GetEnumerator();
// }
}
}
Upvotes: 0
Views: 935
Reputation: 101142
Your class has to implement IList
or IListSource
, as the error message tells you.
The class you postet implements IList<T>
, but not IList
.
So you have to implement IList
yourself. You can do it explicitly, so you can't accidently add an object
that is not a Task
(since the non-generic Add
method will be hidden), as long as you don't explicitly cast your TaskCollection
to IList
and call the non-generic Add
method.
public class TaskCollection : IList<Task>, IList
{
int IList.Add(object value)
{
// just call the generic version
Add((Task)value);
}
int IList.IndexOf(object value)
{
// just call the generic version
return IndexOf((Task)value);
}
...
}
Upvotes: 3