1

データグリッドのデータソースとして機能するバインディングソースがあります。バインディングソースをバインドした後、バインディングソースの2つの行を交換しようとしています。

 object currentRow = dashBoardBindingSource[index];
                    object aboveRow = dashBoardBindingSource[index - 1];
                    dashBoardBindingSource[index] = aboveRow;
                    dashBoardBindingSource[index - 1] = currentRow;

上記のコードは私に次のような例外を与えます

Cannot set an object into this list.

次のコードを使用して、データをバインディングソースにバインドしています

OnlineOutageReport[] outageReports;
dashBoardBindingSource = new BindingSource();
            dashBoardBindingSource.DataSource = ToDataTable<OnlineOutageReport>(outageReports);


private static DataTable ToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection props =
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        for (int i = 0; i < props.Count; i++)
        {
            PropertyDescriptor prop = props[i];
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
        prop.PropertyType) ?? prop.PropertyType);

        }
        object[] values = new object[props.Count];
        foreach (T item in data)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = props[i].GetValue(item);
            }
            table.Rows.Add(values);
        }
        return table;
    }

誰もが考えを持っています。ありがとう

4

1 に答える 1

2

これは、BindingSource を DataGridView のある行から別の行に移動する方法です。

            int index = this.KeyDataGridView.SelectedRows[0].Index;

            this.KeyDataGridView.ClearSelection();
            if (index == this.KeyDataGridView.Rows.Count - 1)
            {
                return;//can go down
            }
            var tempKey = this.keysBindingSource[index];
            this.keysBindingSource.RemoveAt(index);
            this.keysBindingSource.Insert(index + 1, tempKey);

代わりに dataSource.Insert(index) を使用する必要があるだけかもしれません

于 2015-08-13T21:21:10.437 に答える