Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
DirectoryInfo dir = new DirectoryInfo(@"D:\Temp");
FileInfo[] y = dir.GetFiles();
List<string> asx = y.Select(p => new { File = p.DirectoryName + p.Name }.ToString()).ToList();
datagridview1.datasource=asx;




why list asx showing length: 33,11,20...?????


[edit]Code block added - OriginalGriff
Fixed markup typo - Matt Heffron[/edit]
Posted
Updated 12-Apr-13 13:31pm
v5
Comments
Sergey Alexandrovich Kryukov 12-Apr-13 16:58pm    
Please don't post non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

1 solution

For me, it doesn't:
C#
DirectoryInfo dir = new DirectoryInfo(@"D:\Temp");
FileInfo[] y = dir.GetFiles();
List<string> asx = y.Select(p => new { File = p.DirectoryName + p.Name }.ToString()).ToList();
It returns an array of strings:
C#
foreach (string s in asx)
    {
    Console.WriteLine(s);
    }
Prints:
{ File = D:\TempAA.TXT }
{ File = D:\TempAAdel.jpg }
{ File = D:\TempMyIP.aspx }
{ File = D:\TempMyPic.jpg }
{ File = D:\TempMyText.txt }
{ File = D:\TempTestingSQLite.sldb }
{ File = D:\TempThumbs.db }}
So what am I doing that you aren't, or vice versa?

BTW: You probably want to add a "\\" in there, between the directory and file names.
 
Share this answer
 
Comments
scorpzonex 12-Apr-13 16:09pm    
but when i'm using dataGridView1.DataSource =asx;
it shows the length not the text;
[no name] 12-Apr-13 16:42pm    
Then you would need to show the real actual code that you are using. The code that you posted is not the real code as it would not even compile.
OriginalGriff 13-Apr-13 2:47am    
Ah! It's not the conversion that causes the problem, it's using a List of strings as the DataSource. When you do that, the DataGridView looks at all the public Properties of the object the list is made up of, and provides a column for each of them. The only public property a string has is it's Length, so that is the column you get - a string doesn't have a "Text" property because it is *already* the text! :laugh:
You need to change what you are loading into the DataGridView - try this:
myDataGridView.DataSource = asx.ConvertAll(x => new { Value = x });
It creates a new anonymous type which has a single public property "Value" and assigns the string to that.
scorpzonex 13-Apr-13 9:09am    
Thank you. Yaap that works.
OriginalGriff 13-Apr-13 9:16am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900