Reputation: 810
I'm getting the following error when trying to add a new row to an Infragistic UltraWinGrid with a datasource of type BindingList:
Unable to add a row: Constructor on type 'System.String' not found
Deleting items via the grid works just fine but, the above error occurs when trying to use the add new row at the bottom of the grid.
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();
}
}
I tried adding a button to manually add an item to the list without involving the UltraWinGrid (btnAddCookies_Click) and this worked without issue.
Any ideas to help push me in the right direction? Thanks
Upvotes: 0
Views: 2479
Reputation: 3513
A BindingList<string>
with AllowNew
to true
causes the bindinglist to create new items by reflection. What the bindinglist does is instantiate the generictype ( in this case string ) through reflection. This is also was causes the exception. Because the string valuetype doesnt have any constructor. Binding a own created type with a constructor is surely possible. In this case you can create a wrapper class:
public class MyList
{
public string ListItem { get; set; }
public MyList( string listItem )
{
ListItem = listItem;
}
}
And bind this class : BindingList<MyList> bindingList = new BindingList<MyList>();
Upvotes: 1