Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:14:57+00:00 2026-06-15T12:14:57+00:00

public class myRows { public decimal Col1 { get; set; } public decimal Col2

  • 0
 public class myRows
        {
            public decimal Col1 { get; set; }
            public decimal Col2 { get; set; }
            public decimal Col3 { get; set; }
            public decimal Col4 { get; set; }
            public decimal Col5 { get; set; }
            public decimal Col6 { get; set; }
            public string myDateTimeCol { get; set; }

            public myRows(string str)
            {
                var fields = str.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                Col1 = Convert.ToDecimal(fields[0]);
                Col2 = Convert.ToDecimal(fields[1]);
                Col3 = Convert.ToDecimal(fields[2]);
                Col4 = Convert.ToDecimal(fields[3]);
                Col5 = Convert.ToDecimal(fields[4]);
                Col6 = Convert.ToDecimal(fields[5]);
                myDateTimeCol = string.Format("{0} {1} {2} {3} {4}", fields[6], fields[7], fields[8], fields[9], fields[10]);
            }
        }

I then read my LogFile like

var rows = new List<myRows>();
var sr = new StreamReader(txtFileToImport.Text);

while (!sr.EndOfStream)
       {
          string s = sr.ReadLine();
           if (!String.IsNullOrEmpty(s.Trim()))
               {
                  rows.Add(new myRows(s));
                }
        }

sr.Close();
dataGridView_preView.DataSource = rows;

enter image description here

The problem am facing with this code is that, if the input LogFile has more than 12 or more columns, i get an index out of range exception.

Is there away i can re-factor the code to make it process any LogFile with any number of columns. The Log-files to be processed have varying number of columns, the only guarantee i have is that the date column(s) will always be the last column(s) in all cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:14:58+00:00Added an answer on June 15, 2026 at 12:14 pm

    I have a totally different approach to this without using any Lists. Please excuse the coding, this is just a quick test.

    First of all, I’m going to use a DataTable.

    public DataTable dt = new DataTable();
    

    The Data I read in from each line will be stored in an array of Type String.

    public string[] data;
    

    First thing we need to do is establish how many columns are in our data file. Below I count the number of instances and then generate a new the number of columns required.

    public void addcolumns()
            {
    
                using (StreamReader reader = new StreamReader(@"C:\DataColumnTest.txt"))
                {
                    string s = reader.ReadLine();
                    string[] col = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    
    
                    foreach (var a in col)
                    {
                        dt.Columns.Add(new DataColumn());
                    }
                }
            }
    

    Next up, Where going to have to add the rows our DataTable. The number of columns in our row data should always match the number of columns in our DataTable and as where doing this initially it should work every time :-

    Public void addRows()
        {
            var sr = new StreamReader(@"C:\DataColumnTest.txt");
    
            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                if (!String.IsNullOrEmpty(s.Trim()))
                {
                    data = null;
                    data = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    dt.Rows.Add(data);
                }
    
            }
    
            sr.Close();
        }
    

    All ive done for this quick example is call these methods in form Load, and then add the datasource for the DataGrid :-

     private void Form1_Load(object sender, EventArgs e)
            {
    
                addcolumns();
                addRows();
                dataGridView1.DataSource = dt;
            }
    

    I know there will be cleaner ways of doing this, but it does work, unlike the other examples I’ve seen posted.

    Hope This Helps.

    *** EDIT ****

    Here is the Data I used, along with Result.

    enter image description here

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class myRows { public decimal Number1 { get; set; } public decimal Number2
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Emp { public int ID { get; set; } public string Name
public class Abbreviation { public string ShortName { get; set; } public string LongName
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
public class Base{ protected String str; public static final Base ERROR = new Base(error);
public class SearchForm { //Note: Property is nullable public DateTime? CurrentViewDate {get;set;} public DateTime
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.