データグリッドのデータソースとして機能するバインディングソースがあります。バインディングソースをバインドした後、バインディングソースの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;
}
誰もが考えを持っています。ありがとう