当尝试使用类型为UltraWinGrid的数据源将新行添加到不片段化UltraWinGrid时,我会得到以下错误:
无法在“System.String”类型上添加行:构造函数未找到
通过网格删除项目很好,但是,当试图使用网格底部的add新行时,会出现上述错误。
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)的情况下手动将一个项添加到列表中,这是没有问题的。
有什么办法能把我推向正确的方向吗?谢谢
发布于 2014-12-30 22:55:28
BindingList<string>与AllowNew到true将导致绑定列表通过反射创建新项。bindinglist所做的是通过反射实例化泛型类型(在本例中是字符串)。这也是引起异常的原因。因为字符串值类型没有任何构造函数。当然,用构造函数绑定自己创建的类型是可能的。在本例中,您可以创建一个包装类:
public class MyList
{
public string ListItem { get; set; }
public MyList( string listItem )
{
ListItem = listItem;
}
}并绑定这个类:BindingList<MyList> bindingList = new BindingList<MyList>();
https://stackoverflow.com/questions/27713623
复制相似问题