By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

fill dropdownlist with filename

P: n/a
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\\Docum ents");
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
--
thanks,

Remco Ploeg
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Remco Ploeg" <Re********@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
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\\Docum ents");
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
--
thanks,

Remco Ploeg

Nov 19 '05 #2

P: n/a
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/tg...glance&s=books

Nov 19 '05 #3

P: n/a
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;
}
--
thanks,

Remco Ploeg
"ha**********@gmail.com" wrote:
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/tg...glance&s=books

Nov 19 '05 #4

P: n/a
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\\Docum ents");
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
--
Thanks,
$.Trundle
MCSE

"Remco Ploeg" wrote:
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\\Docum ents");
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
--
thanks,

Remco Ploeg

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.