I'm working on a desktop application in C# that reads a directory and displays in a DataGridView the directory entries of the PDFs found in that directory, like a File Explorer that only shows PDFs. The user will need to be able to rearrange the order of the files in the DataGridView by dragging and dropping (single row select, not multi). This reordering is not 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. After this, the user has the application process the PDFs in the rearranged order by looping through the rows of the DataGridView.

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 can create an array of objects, one object per column, and store the array 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. Can you give a brief rundown of 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?

  • 3
    "Can you give a brief rundown of each?" This easily googled and not an on-topic question. Check MSDN and you'll get heaps of information, on top of plenty of online guides and tutorials elsewhere. – Flater Oct 26 at 0:05
  • Again, the community seeks for reasons to close and downvote a question instead of giving constructive feedback (except for Flater above, which is IMHO about a minor, unimportant part of the question text). I think the main question is fine, focussed enough for a comprehensive answer. – Doc Brown Oct 26 at 12:10
  • The question is not really about "a data structure"; it is about "which WinForms DataGridView UI control and options would work best for achieving (mimicking) a particular desired effect. To call it a "data structure" is akin to saying that every class is a data structure, which is not how programmers see things in computer science. – rwong Oct 27 at 19:09
  • Thanks all for the input. @Flater I meant a brief rundown of the pros and cons in the context of the requirements. – RobertSF Oct 31 at 17:16

Using a DataGridView in unbound mode is a possible solution for this use case. I would recommend to initialize it's DataSource with a BindingSource, which is initialized with an object of type System.ComponentModel.BindingList<T>, where T is a class provided by yourself representing the objects / rows in managed by your Explorer-like application.

However, this may be overdesigned. We implemented such an application some time ago using a derivation of a System.Windows.Forms.ListView (with the property View set to details, to get column headings), which is more lightweight and maybe sufficient for your case as well. The rows in this control have to be ListViewItem objects, and data objects can be associated with those ListViewItem by utlizing the Tag property.

Here is a tutorial by Microsoft showing how to create such an application.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.