I have several DataGridView
s which perform similar operations on different types of data. For one of these operations, I want to select a row in a DataGridView
and move it up or down one index. These grids are data-bound to a BindingList<T>
where T
is the object type of interest for each DataGridView
. Operations performed on the DataGridView
should be reflected in the data-source.
The reorder operation is called from a click event, sent by a Button
in a DataGridView
. The Button
object and functionality is shared between all grids and is added to a grid's control when the mouse hovers over said grid. The function call is as follows:
protected void UpButtonClick(object sender, EventArgs e)
{
DataGridView parent = (sender as Button).Parent as DataGridView;
/* Code to move row up */
}
If the following code is used to reorder the rows, a System.InvalidOperationException: 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.'
occurs because each grid is data-bound.
DataGridViewRow row = parent.Rows[gridRow];
parent.Rows.RemoveAt(gridRow);
parent.Rows.Insert(gridRow - 1, row);
Trying to modify the data-source is problematic too, because the type of the elements is unknown. Casting to type BindingList<T>
requires that T
be specified. Similarly, BindingList
's inheritance and implements all involve declaration of element type.
object row = (parent.DataSource as BindingList)[gridRow];
(parent.DataSource as BindingList).RemoveAt(gridRow);
(parent.DataSource as BindingList).Insert(gridRow - 1, row);
Furthermore, it seems like poor code to create a collection of types and index each grid to specify which type corresponds to which data-source. Since the operations in question are not dependent on element type, and furthermore, do not mix element types between grids, it should stand to reason that a generic data source should be able to be reordered without knowing additional details about the data source.
How can I programmatically reorder elements in a data-bound DataGridView
or BindingList
without explicitly knowing the data type of each element?