简体   繁体   中英

How To Move Row In DataBound DataGrid In C# without Knowing The Object Type

I have a number of datgridgs that I want to be able to reorder rows for. They are all SortableBindingList<> : List<> types but they contain different objects. I tried casting the DataGridView's DataSource to SortableBindingList<object> to .RemoveAt() & .Insert() but the cast failed. I tried to pass the object type into the function using a Type but that failed.

Type objType;
...
var x = (SortableBindingList<objType>) dataGridView.DataSource;

but that doesn't work either, nether does 'typeof(objType)`.

Not sure how to proceed.

Turns out that I can do this with dynamic types.

            dynamic list = dgv.DataSource;
            var item = list[rowIndexFromMouseDown];
            list.RemoveAt(rowIndexFromMouseDown);
            if (rowIndexOfItemUnderMouseToDrop == -1)
                list.Add(item);
            else list.Insert(rowIndexOfItemUnderMouseToDrop, item);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM