简体   繁体   English

当控件是集合数据源上的数据绑定错误时,不能以编程方式将行添加到DataGridView的rows集合中

[英]Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound error on collection data source

I know there are threads on this error but i only found solutions in case the data source is a table. 我知道有关于此错误的线程,但是我只在数据源是表的情况下找到了解决方案。 In my case the data source is a list. 就我而言,数据源是一个列表。 Here is my code: 这是我的代码:

    private void AdminForm_Load(object sender, EventArgs e)
    {
        dgUser.DataSource = read.getUsers();
        dgUser.Rows.Add();
    }

So apparently the Add() method doesn't work for collections. 因此,显然Add()方法不适用于集合。 Any solutions? 有什么办法吗?

List<String> list = new List<String>();
list.add("val1");
dataGridView1.DataSource = list;
list.add("val2");
dataGridView1.DataSource = null;
dataGridView1.DataSource = list;

In this case you have to set datasource to null, and then again to list; 在这种情况下,您必须将数据源设置为null,然后再次将其设置为list; Or better to use Binding list 或者更好地使用绑定列表

BindingList <String> list = new BindingList<String>();
list.Add("val1");
dataGridView1.DataSource = list;
list.Add("val1");

In this case you dont have to "refresh" anything, its done automatically 在这种情况下,您不必“刷新”任何内容,它会自动完成

Try code below. 请尝试以下代码。 Additional you can check collection type in line 4. 另外,您可以在第4行中检查收集类型。

private bool AddNewRow(DataGridView grid)
{
    var collection = grid.DataSource;
    if (collection == null)
    {
        grid.Rows.Add();
        return true;
    }

    var itemType = GetCollectionItemType(collection.GetType());
    if (itemType != null && itemType != typeof(object) && !itemType.IsAbstract && itemType.GetConstructor(Type.EmptyTypes) != null)
    {
        try
        {
            dynamic item = Activator.CreateInstance(itemType);
            ((dynamic)collection).Add(item);

            if (!(collection is System.ComponentModel.IBindingList))
            {
                grid.DataSource = null;
                grid.DataSource = collection;
            }

            return true;
        }
        catch { }
    }

    return false;
}
public static Type[] GetGenericArguments(this Type type, Type genericTypeDefinition)
{
    if (!genericTypeDefinition.IsGenericTypeDefinition)
        return Type.EmptyTypes;

    if (genericTypeDefinition.IsInterface)
    {
        foreach (var item in type.GetInterfaces())
            if (item.IsGenericType && item.GetGenericTypeDefinition().Equals(genericTypeDefinition))
                return item.GetGenericArguments();
    }
    else
    {
        for (Type it = type; it != null; it = it.BaseType)
            if (it.IsGenericType && it.GetGenericTypeDefinition().Equals(genericTypeDefinition))
                return it.GetGenericArguments();
    }

    return new Type[0];
}
public static Type GetCollectionItemType(Type collectionType)
{
    var args = GetGenericArguments(collectionType, typeof(IEnumerable<>));
    if (args.Length == 1)
        return args[0];

    return typeof(IEnumerable).IsAssignableFrom(collectionType)
        ? typeof(object)
        : null;
}
   var dataSource = datagrid.DataSource;
                    (dataSource as IList).Add(obj);
                    datagrid.DataSource = null;
                    datagrid.DataSource = dataSource;
                    datagrid.Refresh();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 当控件为数据绑定时,无法以编程方式将行添加到 datagridview 的行集合中 - Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound 当控件为数据绑定时,无法以编程方式添加到 DataGridView 的行集合中 - Cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 错误“当控件绑定数据时,行无法以编程方式添加到datagridview的行集合中” - Error “Rows cannot be programatically added to datagridview's row collection when the control is data-bound” 控件数据绑定时无法在DataGridView中以编程方式添加行 - Rows cannot be added programmatically in the DataGridView when the control data-bound 行不能以编程方式添加到DataGridView的行集合中 - Rows cannot be programmatically added to the DataGridView's rows collection 无法将行动态添加到我的数据绑定dataGridview中 - Cannot add rows to my data-bound dataGridview dynamically XML加载后,DataGridView无法添加行,因为“控件绑定了数据” - DataGridView cannot add rows as “control is data-bound” after XML load 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 带有Grid.Rows.Add方法的C#DataGridView数据绑定错误 - C# DataGridView data-bound error with Grid.Rows.Add method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM