Lista de archivos en el directorio con paginación y búsqueda alfa

I am in the process of makeing a small script that will take all files from a pre-defined folder on the server, list them, sorted by name, with a pager and a way of grouping them by [A], [B] etc. navigation.

My thoughs were to loop through all the files and put them in a Array/List/Directonary (?) and then make a Gridview/Datagrid/Listview (?) to show the files, and handle the default paging.

Looking for inputs on the best way to achieve this based on the above - any inputs and/or snippets/examples would be much appreciated.

preguntado el 09 de marzo de 12 a las 13:03

1 Respuestas

If you want more flexibility this is easy to do with LINQ.

var pageSize = 25;
var pageNum = 1;

var currentPage = Directory.GetFiles("dir_path").Skip((pageNum - 1) * pageSize).Take(pageSize);

// Do something with the results.

De/Incrementing pageNum on each next/prev (just make sure you take 0 & the max page num into consideration).

You can also include other linq operations like orderby, where, select, groupby, join, etc.

De tu comentario:

In the above example you can call methods on currentPage like .ToArray() / .ToList() / .ToDictionary() in order to form the data into the collection that best suits your needs.

An example using System.Windows.Forms.ListView:

Order the items, convert them to an array of ListViewItem and then use AddRange() on the list view's item collection to add them to it.

var myListView = new ListView();
var allItems = currentPage.OrderBy(c => c).Select(c => new ListViewItem(c)).ToArray();

respondido 09 mar '12, 20:03

I like the idea of using LINQ for this task, but i am unsure where to go next. Could i for example use "currentPage" from the above example, directly as a datasource for a ListView? - hermano

@brother Made some additions to the post. Hope that helps. - Tiempo de espera

thx for the update - i'm getting closer :-) I tried the above example, but i am getting " CS1502: The best overloaded method match for 'System.Web.UI.WebControls.ListViewItem.ListViewItem(System.Web.UI.WebControls.ListViewItemType)' has some invalid arguments" in line "var allItems = currentPage.OrderBy(c => c).Select(c => new ListViewItem(c)).ToArray();". Any idea what could be causing this? - hermano

I am also getting " CS1061: 'System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem>' does not contain a definition for 'AddRange' and no extension method 'AddRange' accepting a first argument of type 'System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem>' could be found (are you missing a using directive or an assembly reference?)" from the AddRange method - is it me who is missing something here? - hermano

My example was, just an example. It was using System.Windows.Forms.ListView. Not the System.Web... version. You'll need to mold the example to your specific needs. - Tiempo de espera

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.