Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
Good day,
I have reader text file but the length of file not static
my code is :
C#
public class jr
        {




            public string name1 { get; set; }
            public string name2 { get; set; }
            public string name3 { get; set; }
            public string name4 { get; set; }
            public string name5 { get; set; }
            public string name6 { get; set; }
            public string name7 { get; set; }
            public string name8 { get; set; }
            public string name9 { get; set; }
           

            public static List<jr> LoadUserListFromFile(string path)
            {


                var users = new List<jr>();


                foreach (var line in File.ReadAllLines(path))
                {

               


                    var delimiters = new char[] { ',' };

                    var columns = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                    users.Add(new jr




                    {

                        name1 = columns[0].Trim(),
                        name2 = columns[1].Trim(),
                        name3 = columns[2].Trim(),
                        name4 = columns[3].Trim(),
                        name5 = columns[4].Trim(),
                        name6 = columns[5].Trim(),
                        name7 = columns[6].Trim(),
                        name8 = columns[7].Trim(),
                        name9 = columns[8].Trim(),
                       
                    }

                );

                }
                return users;

            }

        }



to read it in datagridview :

C#
dataGridView1.DataSource = jr.LoadUserListFromFile("file path");



for some of files i can read but another files i received error index was outside the bounds of the array
so i need to make my code dynamic not static for specific files.
I mean like check the length of file and create columns as the length

I hope every things is clear.
Thank you for your help.
Posted
Updated 27-Jul-15 2:39am
v2

1 solution

You assume columns has index 0 to 8 available. Maybe check first if this is the case using columns.Length.

Good luck!
 
Share this answer
 
Comments
Jerji Rani 27-Jul-15 9:05am    
Yes , my point make those columns length automatic if the file 20 index it will be create in datagridview automatically
thank you for your answer
E.F. Nijboer 27-Jul-15 9:38am    
You can add DataColumns to the dataSource and fill it up. Check out the link. The answer you look for is in te question :-D

http://stackoverflow.com/questions/25738487/giving-csv-as-a-datasource-to-chart
E.F. Nijboer 27-Jul-15 9:39am    
Same example here as answer:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/8a2179c2-814d-4b1f-a3ef-6cda0f805168/sorting-individual-coumns-in-datatable?forum=csharpgeneral
Jerji Rani 27-Jul-15 10:14am    
Thank you for your help
i will check if it will help me
thank you again
Jerji Rani 27-Jul-15 13:45pm    
its not working for me i recived another error
same name for the column name
any one have another solution ?
thank you for your help

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