【问题标题】:Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound error on collection data source当控件是集合数据源上的数据绑定错误时,无法以编程方式将行添加到 DataGridView 的行集合
【发布时间】:2014-05-11 18:05:34
【问题描述】:

我知道这个错误有线程,但我只在数据源是表的情况下找到了解决方案。 在我的情况下,数据源是一个列表。这是我的代码:

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

显然 Add() 方法不适用于集合。有什么解决办法吗?

【问题讨论】:

  • 你必须追加列表,然后再次绑定你的datagridview。

标签: c# datagridview


【解决方案1】:
List<String> list = new List<String>();
list.add("val1");
dataGridView1.DataSource = list;
list.add("val2");
dataGridView1.DataSource = null;
dataGridView1.DataSource = list;

在这种情况下,您必须将 datasource 设置为 null,然后再设置为 list; 或者更好的使用绑定列表

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

在这种情况下,您不必“刷新”任何内容,它会自动完成

【讨论】:

  • 第一个不起作用,但第二个起作用。谢谢你,好心的先生。
【解决方案2】:

试试下面的代码。另外,您可以在第 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;
}

【讨论】:

    【解决方案3】:
       var dataSource = datagrid.DataSource;
                        (dataSource as IList).Add(obj);
                        datagrid.DataSource = null;
                        datagrid.DataSource = dataSource;
                        datagrid.Refresh();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2014-09-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多