【问题标题】:Cannot Convert IList To Binding List无法将 IList 转换为绑定列表
【发布时间】:2014-08-12 11:10:46
【问题描述】:

我正在开发一个应用程序,其中我将Object 转换为IList。然后我想将其转换为BindingList

这是我正在尝试的代码

IList data = spareEntity.getall() as IList;
var listBinding = new BindingList<IList>(data);

public object getall()
{
    using (var worldbankAMSentitiesNew = new WorldBankAMSEntities())
    {
        var q = from fullSpareInventory in worldbankAMSentitiesNew.Spare_Inventory
        select new
        {
            fullSpareInventory.Is_Spare,
            fullSpareInventory.Manufacturer,
            fullSpareInventory.Product_No,
            fullSpareInventory.Remarks,
            fullSpareInventory.Remedy_Ticket_No,
            fullSpareInventory.Serial_No,
            fullSpareInventory.Spare_Type,
            fullSpareInventory.Spare_Inventory_Volume.Spare_Name,
            fullSpareInventory.Spare_ID,
            fullSpareInventory.Assigned_Date,
            fullSpareInventory.Description,
            fullSpareInventory.Spare_Volume_ID
        };
        return q.ToList();
    }
}

【问题讨论】:

  • 我对 Winform 比较陌生,所以请解释我哪里错了
  • getall方法返回的元素是什么类型的?
  • 获取所有返回对象
  • 它是什么类型的列表?

标签: c# winforms bindinglist


【解决方案1】:

你可以尝试如下:-

var yourList = spareEntity.getall() as List<Object>;
var listBinding = new BindingList<Object>(yourList);

【讨论】:

  • 无效的表达式项
  • 如果你把完整的异常或者你得到错误的确切位置放在@user3859356 上会很好
  • 应该是as List&lt;Object&gt;;(即没有括号)
  • @Default 是正确的,因为你使用的是'as'语法,所以不能使用括号。
  • 是更新了 @user3859356 删除括号其错字并重试
【解决方案2】:
new BindingList<IList>(data);

我认为这里的 IList 类型是错误的。使用 IList 数据使用的泛型类型。备用?什么的……

尝试对象

new BindingList<object>(data);

改变你的 getall()

public IList<object> getall()

var data = spareEntity.getall();

【讨论】:

  • 错误 1 ​​'System.ComponentModel.BindingList.BindingList(System.Collections.Generic.IList)' 的最佳重载方法匹配有一些无效参数
  • On q.toList 错误 4 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IList”。存在显式转换(您是否缺少演员表?)
  • 它给出了这个错误 错误 1 ​​无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IList”。存在显式转换(您是否缺少演员表?)
  • 我认为在您使用匿名类型时,这在强制转换方式上是不可能的
  • 是的,但你不能将匿名类型转换为对象......这就是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多