【问题标题】:UltraWinGrid bound to BindingList<string>, Add Row ErrorUltraWinGrid 绑定到 BindingList<string>,添加行错误
【发布时间】:2014-12-30 22:42:28
【问题描述】:

我在尝试使用 BindingList 类型的数据源向 Infragistic UltraWinGrid 添加新行时遇到以下错误:

无法添加行:找不到类型“System.String”的构造函数

通过网格删除项目可以正常工作,但是在尝试使用网格底部的添加新行时会出现上述错误。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace BindingListChangedEvent
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private BindingList<string> shoppingList;

        private void FrmMain_Load(object sender, EventArgs e)
        {           
            //let's just add some items to the list
            string[] items = { "Rice", "Milk", "Asparagus", "Olive Oil", "Tomatoes", "Bread" };
            shoppingList = new BindingList<string>(items.ToList());

            shoppingList.AllowNew = true; //if this is not set error: "Unable to add a row: Row insertion not supported by this data source" is thrown

            ugList.DataSource = shoppingList;
            shoppingList.ListChanged += new ListChangedEventHandler(ShoppingList_ListChanged);
        }

        public void ShoppingList_ListChanged(object sender, ListChangedEventArgs e)
        {
            MessageBox.Show(e.ListChangedType.ToString()); //what happened to the list?
        }

        private void ugList_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            e.Layout.Override.CellClickAction = CellClickAction.RowSelect;
            e.Layout.Override.AllowDelete = DefaultableBoolean.True;
            e.Layout.Override.AllowAddNew = AllowAddNew.TemplateOnBottom;
        }

    private void btnAddCookies_Click(object sender, EventArgs e)
    {
        shoppingList.Add("Cookies");
        ugList.Refresh();
    }
}

我尝试在不涉及 UltraWinGrid (btnAddCookies_Click) 的情况下添加一个按钮以手动将项目添加到列表中,这没有问题。

有什么想法可以帮助我朝着正确的方向前进吗?谢谢

【问题讨论】:

    标签: c# winforms infragistics


    【解决方案1】:

    BindingList&lt;string&gt;AllowNewtrue 导致绑定列表通过反射创建新项目。 bindinglist 所做的是通过反射实例化泛型类型(在本例中为 string )。这也是异常的原因。因为字符串值类型没有任何构造函数。将自己创建的类型与构造函数绑定当然是可能的。在这种情况下,您可以创建一个包装类:

        public class MyList
        {
            public string ListItem { get; set; }
    
            public MyList( string listItem )
            {
                ListItem = listItem;
            }
        }
    

    并绑定这个类:BindingList&lt;MyList&gt; bindingList = new BindingList&lt;MyList&gt;();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2013-10-31
      • 1970-01-01
      • 2013-07-07
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多