System.ArgumentException: Complex DataBinding acce

2020-08-13 05:16发布

问题:

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()); 

回答1:

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().



标签: c# winforms linq