Home > Mobile >  Fill DataGrid in WPF C# with Directory.GetDirectories(Path)
Fill DataGrid in WPF C# with Directory.GetDirectories(Path)

Time:10-21

I am using code like this:

var s = Directory.GetDirectories(NAS PATH)

Then I fill DataGrid with it like this:

RandomDataGridName.ItemsSource = s

The problem I get is that when using Listbox it works fine but once I use DataGrid I get only the length filled in DataGrid.

So I have done the following:

<DataGrid.Columns>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding }" Header="Name"/>
</DataGrid.Columns>

The above code works but I only get the name column containing the entire path of the folders in NAS.

I would like to get standard date and the rest of the information that Windows keeps about its folders so users can sort directories by date.

To sum up my question:

  1. Can DataGrid benefit from Directory.GetDirectories()?
  2. Can I trim the DataGrid displayed path so the user does not see the full path of the directories; rather just the last names of folders?

CodePudding user response:

you can get more info using DirectoryInfo object:

var folder = new DirectoryInfo(NAS_PATH);
DirectoryInfo[] subdir = folder.GetDirectories();
RandomDataGridName.ItemsSource = subdir;

note DirectoryInfo[] result (instead of string[] from Directory.GetDirectories which simply gives paths)

then configure DataGrid to show relevant properties

<DataGrid.Columns>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding Name}" Header="Name"/>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding CreationTime}" Header="CreationTime"/>
</DataGrid.Columns>
  • Related