Click to See Complete Forum and Search --> : Filter Datagrid/Datasource


doomsday123
August 24th, 2005, 01:20 PM
<code>
Dim dirInfo as New DirectoryInfo(Server.MapPath(""))

articleList.DataSource = dirInfo.GetFiles("*.*")
articleList2.DataSource = dirInfo.GetDirectories("*_*")
articleList.DataBind()
articleList2.DataBind()
</code>

I want to filter the filenames so that it doesnt show index.aspx. Could someone show me how? Thanks.

mmetzger
August 24th, 2005, 01:33 PM
Try:


Dim dirInfo as New DirectoryInfo(Server.MapPath(""))

articleList.DataSource = dirInfo.GetFiles("*.*").Remove("index.aspx");
articleList2.DataSource = dirInfo.GetDirectories("*_*")
articleList.DataBind()
articleList2.DataBind()


EDIT: Ack... nevermind... didn't read the properties correctly.

Probably the easiest way to do this is simply create a datatable on the fly with the entries you're interested in and filter it appropriately.

doomsday123
August 24th, 2005, 01:46 PM
Im sorry. I ment to put this in the VB section. This is actually VB code inbedded into a ASP.Net page. Would you happen to know what the syntax is for VB or should i take this to the other forum?

mmetzger
August 24th, 2005, 01:49 PM
It doesn't really matter, it's all using the framework anyway. But you effectively loop through the FileInfo array, add the appropriate entries to a datatable, and set your DataSource equal to that table.

doomsday123
August 24th, 2005, 02:35 PM
I came up with this and it seems like it will work but when I run it on localhost and the server i get Access to file "index.aspx" is denied.

<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim di as New DirectoryInfo(Server.MapPath(""))
Dim dirs As FileInfo() = di.GetFiles("*.*")
Dim diNext As FileInfo

For Each diNext In dirs
If diNext.Name = "index.aspx"
diNext.Delete()
End If
Next

articleList.DataSource = dirs
articleList2.DataSource = di.GetDirectories("*_*")
articleList.DataBind()
articleList2.DataBind()

End Sub
</script>

doomsday123
August 24th, 2005, 02:40 PM
Well no freakin duhhh lol. Its deleting the file. Is there anyway i can just remove it from the FileInfo so when I bind it to the datagrid it wnt show up?

mmetzger
August 24th, 2005, 04:07 PM
There isn't an exclude filter in the FileInfo, only an inclusive one. Here's something similar I do on a page for my users to access (in C#, but you should be able to translate fairly easily.):


DataTable GetMemberDownloadDataTable(string MemberName)
{
string dir = Server.MapPath("/download/customer/") + MemberName + "\\";
string[] filenames;

DataTable dt = new DataTable();
DataColumn name = new DataColumn("File");

System.Type myDataType;
myDataType = System.Type.GetType("System.DateTime");

DataColumn date = new DataColumn("Date", myDataType);

dt.Columns.Add(name);
dt.Columns.Add(date);

try
{
ArrayList filelist = new ArrayList();
string[] extensions = new string[] {"*.zip", "*.rar", "*.iso", "*.exe", "*.msi", "*.tgz", "*.izp"};

foreach (string extension in extensions)
{
filelist.AddRange(Directory.GetFiles(dir, extension));
}

filenames = (string[])filelist.ToArray(typeof(string));
}
catch (System.Exception ex)
{
return dt;
}

foreach (string file in filenames)
{
FileInfo fi = new FileInfo(file);
DataRow dr = dt.NewRow();
dr["File"] = fi.Name;
dr["Date"] = fi.CreationTime;
dt.Rows.Add(dr);
}

return dt;


Basically, in your case just add a DataView filter to drop what you don't want to see. ie:



DataTable dt = GetMemberDownloadDataTable("TestUser");
DataView dv = dt.DefaultView;
dv.Filter = "File <> 'index.aspx'";

DataGrid.DataSource = dv;