开发者

Binding XML to Combobox

开发者 https://www.devze.com 2023-02-05 01:22 出处:网络
While trying to bind XML to Combobox data source, I am getting an error \"Complex DataBinding accepts as a data source either an IList or an IListSource.\"

While trying to bind XML to Combobox data source, I am getting an error "Complex DataBinding accepts as a data source either an IList or an IListSource."

 XDocument obj = XDocument.Load("Managers.xml");

 comboBox1.DisplayMember = "ManagerDesig";
 comboBox1.ValueMember = "ManagerID";
 comboBox1.DataSource = obj.Descendants("manager").Select(x => new
 {
   ManagerDesig = x.Attribute("desig").Value,
   ManagerID = x.Attribute("id").Value
 });

Managers.xml

<managers>
  <manager id="123" desig="CEO" />
  <man开发者_C百科ager id="234" desig="CFO" />
  <manager id="456" desig="CIO" />
</managers>

Please help


try:

 comboBox1.DataSource = obj.Descendants("manager").Select(x => new
 {
   ManagerDesig = x.Attribute("name").Value,
   ManagerID = x.Attribute("id").Value
 })
.ToList();//convert to list


First, there is a typo in your example: you're retrieving the "name" attribute whereas there are only "id" and "desig" attributes defined.

Second, use ToList extension method like the following:

comboBox1.DataSource = obj.Descendants("manager").Select(x => new
{
    ManagerDesig = x.Attribute("name").Value,
    ManagerID = x.Attribute("id").Value
}).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消