Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource

Tags:

c#

linq

winforms

I'm using the C# code below to populate a WinForms ListBox. I want to hide all System folders however. Like the $RecyclingBin for example. But it gives me the following error.

System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource.

Being new to LINQ this is more than confusing to me. Can anyone tell me where I'm going wrong?

string[] dirs = Directory.GetDirectories(@"c:\");
var dir = from d in dirs
          where !d.StartsWith("$")
          select d;

listBox.DataSource = (dir.ToString()); 
like image 263
JimDel Avatar asked Jul 11 '11 19:07

JimDel


1 Answers

Change:

listBox.DataSource = (dir.ToString()); 

To:

listBox.DataSource = dir.ToList();

dir.ToString() will simply spit out some description of the enumerable, which isn't useful. The error message indicates it needs a list, hence the .ToList().

like image 184
Kirk Woll Avatar answered Oct 12 '22 23:10

Kirk Woll