fill dropdownlist with filename

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
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;
}
 
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
 
Back
Top