Best DataSource for drang-and-drop rearranging of rows in a DataGriveView

Anonymous
2020-10-25T20:20:40.083+00:00

I'm working on an application in C# that reads a directory and displays the directory entries for the PDFs found in that directory in a DataGridView, like a File Explorer that only shows PDFs. The user will need to be able to rearrange the order of the files by dragging and dropping the files (single row select, not multi). This reordering only happens in the DataGridView (nothing is written to disk), and is completely arbitrary, not by date or file name or anything like that. This is why the user reorders the files by dragging and dropping.

I had originally used a DataSet with a DataTable to store the directory contents. This DataTable is bound to the DataGridView with a BindingSource. I used this approach because it let me do a lot in the Visual Studio Designer. However, you can't insert and remove elements from a BindingSource if its bound to a DataTable. You would have to do the inserting and removing at the DataTable, but there's no way to insert a row at a specific location in a DataTable, so basically, I need a different kind of data source for my DVG.

Although Lists are not multicolumn, I know you create an array of objects, one per column, and store the array of objects as a List entry. I know you could also use a Class or a Dictionary. But I don't know the pros and cons of using each.

What about not using an in-memory structure but instead use the DVG in unbound mode, programmatically adding the rows as each directory entry is read? Once the files are ordered as the user desires, whatever processing is done to them will be done with a loop that cycles through the DGV rows, not the rows in the DataSource. Everything I read encourages to use data sources with their DGVs, but is this a use case that calls for unbound mode?

===

Moderators: There are no entries for C#, DataGridView, Windows Forms, or anything like that in these new forums.

Developer technologies | Windows Forms
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2020-10-26T09:10:52.433+00:00

    Hi RobertSF,
    >>but there's no way to insert a row at a specific location in a DataTable
    You can use InsertAt(DataRow, Int32) method to insert a new row into the collection at the specified location.
    And you can try to use a list with a BindingSource.
    Here are some code examples in these threads you can refer to.
    Datagridview drag&drop rows when using datasource
    How could I Drag and Drop DataGridView Rows under each other?
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.