fill dropdownlist with filename

G

Guest

Hi,
I wan to fill a dropdownlist with the filenames in a certain directory, i
use the following statement for retrieving the files:

DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Documents");
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
return (fi.Name);
}

but how can I connect this with the dropdownlist, i need to see there the
file names.

thanks in advance,

Remco
 
H

hashimisayed

Hi,

The way to do this is to set the DataSource property of the
DropDownList with the list of files. Once you do that, then you want to
tell the DropDownList what properties of the FileInfo class it should
use as the Text and Value attributes of the DropDownList. Here is the
code that you need:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// set the path to your directory here
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] fis = di.GetFiles();
DropDownList1.DataSource=fis;
DropDownList1.DataTextField="Name";
DropDownList1.DataValueField="Name";
DropDownList1.DataBind();
}
}


Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/t...f=sr_1_1/102-5068238-6758524?v=glance&s=books
 
E

Eliyahu Goldin

Remco,

Get rid of the foreach statement. Make rgFiles protected. In the .aspx file
write:

<asp:ddl id=ddlFiles runat=server DataSource=<%# rgFiles %>
DataTextField=Name />

Eliyahu
 
G

Guest

Hi,

i created the following code and that's working:

private void loadFilesDataSet ()
{
string [] files = Directory.GetFiles
("C:\\inetpub\\wwwroot\\aspnet\\Documents\\");
object [] values;
System.Data.DataTable filestable = new DataTable ("Files");
filestable.Columns.Add (new DataColumn ("Filename", typeof
(string)));

for (int counter = 0; counter < files.Length; counter++)
{
if (files [counter].EndsWith (".doc") || files
[counter].EndsWith (".doc"))
{
values = new object [] {Path.GetFileName (files
[counter])};
filestable.Rows.Add (values);
}
}

DataSet test = new DataSet ("Filenames");
test.Tables.Add (filestable);
DataView abc = test.Tables [0].DefaultView;
DropDownList4.DataSource = abc;
DropDownList4.DataTextField =
abc.Table.Columns["Filename"].ColumnName.ToString();
DropDownList4.DataBind();

return;
}
 
G

Guest

Hi,
One idea would be to build a HashTable or a SortedList object, inside your
for-each loop, then DataBind() the object to your control in the normal
fashion.

so it would look something like this;
#######################
DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Documents");
var oSList as New SortedList
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
oSList.Add(fi.Name, fi.Name);
}
oSList.TrimToSize()
return oSList;

#######################

and then just DataBind() it to your page control as normal;

#######################
myDropDownList.DataSource=oSList
myDropDownList.DataValueField="Key"
myDropDownList.DataTextField="Value"
myDropDownList.DataBind()
#######################

peace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top