-1

I tried below code to retrieve directory name but it shows Length?

List<string> merchants = new List<string>();
foreach (string i in Directory.GetDirectories(Directory.GetCurrentDirectory() + "/Data/").ToList())
{
   merchants.Add(i);
}
merchantTable.ItemsSource = merchants;

result is

|Length|
_______________________
|20    |
|29    |

i tried below answers but same problem

How to get Directories name

Getting the folder name from a path

WPF Code

<DataGrid x:Name="merchantTable" HorizontalAlignment="Left" Margin="15,39,0,0" VerticalAlignment="Top" Width="208" Height="343"/>

Update

i use MessageBox it shows Returned values are Full path like C:\.....\Data\.....

A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
  • The code works fine - it returns the full path for all directories under "Data" (set a breakpoint after the `foreach` loop and examine the `merchants` array). The problem is in your table binding - it looks like you're binding to the `Length` property of each string somewhere. – Rufus L Sep 24 '19 at 21:54
  • @RufusL wpf code assigned .edit again . – A Farmanbar Sep 24 '19 at 21:57
  • What does your columns look like? – Sach Sep 24 '19 at 21:58
  • @Sach all of my tables get List shema all works fine .but it doesn't – A Farmanbar Sep 24 '19 at 21:59
  • You have auto-generated columns, one for each property of the data item. Length is the only property of the string class. – Clemens Sep 24 '19 at 22:00
  • No, I mean on your `DataGrid`. Either you need to specify columns, or you need to set `AutoGenerateColumns` property to `true`. – Sach Sep 24 '19 at 22:00
  • @Sach OP does already have auto-generated columns. – Clemens Sep 24 '19 at 22:01
  • Use a ListBox instead of a DataGrid. And simplify your code to `merchantTable.ItemsSource = Directory.GetDirectories(...)` – Clemens Sep 24 '19 at 22:01
  • `merchantTable.ItemsSource = Directory.GetDirectories(Path.Combine(Directory.GetCurrentDirectory(), "Data"));` would be a shorter way to write that code. – Rufus L Sep 24 '19 at 22:02
  • @Clemens ListBox worked !! can you post an answer ? why DataGrid not worked? – A Farmanbar Sep 24 '19 at 22:08

2 Answers2

1

A DataGrid is meant to show a set of columns, usually one for each property of the data item class, i.e. the element type of its ItemsSource collection.

Hence it doesn't make sense to use it for a collection of strings. Use a ListBox instead

<ListBox x:Name="merchantTable"/>

and simplify the ItemsSource assignment:

merchantTable.ItemsSource = Directory.GetDirectories(
     Path.Combine(Directory.GetCurrentDirectory(), "Data"));

If you really need to use a DataGrid, define a column like this:

<DataGrid x:Name="merchantTable" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Directory">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
Clemens
  • 123,504
  • 12
  • 155
  • 268
0

This code makes nearly no sense; you call a method that returns an array of string, that you then convert to a list, that you then add one by one to another list..

Anyhow, I think you then did something like set DisplayMemberPath or BindingPath to @Length and now your list control is showing you the lengths of all the strings in the list - do a debug print of merchants[0] and it will be a string that is 20 characters long. Data binding typically only binds to properties and a string only has one property. If you want to bind to the string itself you'll probably need to bind the column to a path of .

If you're wanting to show a list of folder names you'll need to call

merchants.Add(Path.GetFileName(i.TrimEnd(Path.DirectorySeparatorChar)));

When GetFilename is used on a path like c:\temp\myfolder it returns MyFolder

the Trim() is there because if you call GetFilename on c:\temp\myfolder\ you get an empty string because it literally just returns everything after the last slash. (Which is nothing)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Yes, you've confirmed what I was thinking : your path is a string 20 characters long and you've bound to the length rather than the string. I think there isn't much else to recommend other than adjust your bindings? – Caius Jard Sep 24 '19 at 22:11
  • See https://stackoverflow.com/questions/28991304/how-to-display-a-list-of-strings-as-a-datagrid-table-in-xaml-and-c-sharp for example – Caius Jard Sep 24 '19 at 22:12
  • I tested your code but it doesn't work with DataGrid – A Farmanbar Sep 24 '19 at 22:12
  • You set `Binding="{Binding Path=.}"` in your data grid column? – Caius Jard Sep 24 '19 at 22:15
  • no i didn't is it necessary ? cuz i didn't set binding for other DataGrids – A Farmanbar Sep 24 '19 at 22:17
  • all of them works normally .show data and columns fine . – A Farmanbar Sep 24 '19 at 22:18
  • I meant what kinds of objects were the other grids showing? Probably not plain strings. Maybe instead of making a list of string you could make a new list of anonymous or dedicated type with a relevant property: `merchantTable.ItemsSource = Directory.GetDirectories( Path.Combine(Directory.GetCurrentDirectory(), "Data")).Select(x => new { Dirname: Path.GetFileName(x) }).ToList();` and rely on the default binding behavior – Caius Jard Sep 24 '19 at 22:22