Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > file system browser
 

file system browser

can some one tell me how to iterate through a direcotries and sub directories by selecting a directory ro in a grid i got the in fo in the grid like this but cant seem to figure out the selected index change or something its killing me,

this is what i have so far

protected void Page_Load(object sender, EventArgs e)

{

DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);

DataTable dtFileInfo = new DataTable();

string StartingDir = @"C:\";

txtCurrentDir.Text = "Current Directory" + " " + StartingDir;

//Create the headers

dtFileInfo.Columns.Add("Name");

dtFileInfo.Columns.Add("Extension");

dtFileInfo.Columns.Add("Size");

dtFileInfo.Columns.Add("Last Modified");

//add columns

foreach (FileInfo f in di.GetFiles())

{

DataRow drFile = dtFileInfo.NewRow();

drFile["Name"] = f.FullName;

drFile["Extension"] = f.Extension;

drFile["Size"] = f.Length;

drFile["Last Modified"] = f.LastAccessTime;

dtFileInfo.Rows.Add(drFile);

//Bind to the grid

GridView1.DataSource = dtFileInfo;

GridView1.DataBind();

}

crash33  Tuesday, August 08, 2006 8:38 PM

Hi,

try this:

DataTable dtFileInfo = new DataTable();
string startingDir = @"C:\";
txtCurrentDir.Text =
"Current Directory" + " " + startingDir;

//Create the headers
dtFileInfo.Columns.Add("Name");
dtFileInfo.Columns.Add(
"Extension");
dtFileInfo.Columns.Add(
"Size");
dtFileInfo.Columns.Add(
"Last Modified");

//add columns
foreach (string file in Directory.GetFiles(startingDir, "*.*", System.IO.SearchOption.TopDirectoryOnly))
{
   
FileInfo f = new FileInfo(file);
   
DataRow drFile = dtFileInfo.NewRow();
   
drFile[
"Name"] = f.FullName;
   
drFile[
"Extension"] = f.Extension;
   
drFile[
"Size"] = f.Length;
   
drFile[
"Last Modified"] = f.LastAccessTime;
   
dtFileInfo.Rows.Add(drFile);
}

//Bind to the grid
GridView1.DataSource = dtFileInfo;

It's a variation of your code which I hope will give you some ideas...

Anyway, to catch selection change, try something like:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
   
if (dataGridView1.SelectedCells.Count == 0)
   
{
       
return;
   
}
   
string selectedFilename = dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
}

Andrej

Andrej Tozon  Tuesday, August 08, 2006 9:10 PM

Hi,

try this:

DataTable dtFileInfo = new DataTable();
string startingDir = @"C:\";
txtCurrentDir.Text =
"Current Directory" + " " + startingDir;

//Create the headers
dtFileInfo.Columns.Add("Name");
dtFileInfo.Columns.Add(
"Extension");
dtFileInfo.Columns.Add(
"Size");
dtFileInfo.Columns.Add(
"Last Modified");

//add columns
foreach (string file in Directory.GetFiles(startingDir, "*.*", System.IO.SearchOption.TopDirectoryOnly))
{
   
FileInfo f = new FileInfo(file);
   
DataRow drFile = dtFileInfo.NewRow();
   
drFile[
"Name"] = f.FullName;
   
drFile[
"Extension"] = f.Extension;
   
drFile[
"Size"] = f.Length;
   
drFile[
"Last Modified"] = f.LastAccessTime;
   
dtFileInfo.Rows.Add(drFile);
}

//Bind to the grid
GridView1.DataSource = dtFileInfo;

It's a variation of your code which I hope will give you some ideas...

Anyway, to catch selection change, try something like:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
   
if (dataGridView1.SelectedCells.Count == 0)
   
{
       
return;
   
}
   
string selectedFilename = dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
}

Andrej

Andrej Tozon  Tuesday, August 08, 2006 9:10 PM
hey thanx worked great now i just gotta get the header sorts thanx agaain
crash33  Tuesday, August 15, 2006 3:23 PM

You can use google to search for other answers

Custom Search

More Threads

• 2 Bounds DataGridViewComboBoxColumn - Select value from the first should filter the second !
• Problem displaying data in Dropdownlist while writing
• events
• Databinding textbox
• understanding table adapters and binding sources.
• DateTime picker problems
• DataGridViewComboBoxColumn binding
• Run-time connectionString Problem
• two-button custom cell
• How to bind to BindingSource.Current?