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

  • Home
  • SEARCH
  • 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 9155059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:31:41+00:00 2026-06-17T12:31:41+00:00

I am new to window application. I have written a code to read files

  • 0

I am new to window application.
I have written a code to read files inside a folder and read it line by line and find some string and display its count to dataGridView.

I wanted to fill a dataGridview which is not working.
I have tried different ways over the internet but none of them are working.

I know that I might be making a silly mistake.
My code is as follow

  public class FileDetail
    {
        public  string filename;
        public int openConnectionCount;
        public int closeConnectionCount;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        int openConnectionCount = 0;
        int closeConnectionCount = 0;
        List<FileDetail> fileDetails =new List<FileDetail>();
        foreach (string file in Directory.EnumerateFiles(textBox1.Text, "*.*", SearchOption.AllDirectories))
        {
            FileDetail fileDetail =new FileDetail();
            fileDetail.filename = file;
            // Read the file as one string.
            using (StreamReader myFile = new StreamReader(file))
            {

                string line;
                while ((line = myFile.ReadLine()) != null)
                {
                    if (line.Contains(" OpenConnection()")) openConnectionCount++;
                    if (line.Contains(" CloseConnection()")) closeConnectionCount++;
                }
                fileDetail.openConnectionCount = openConnectionCount;
                fileDetail.closeConnectionCount = closeConnectionCount;
            }
            fileDetails.Add(fileDetail);
            openConnectionCount = 0;
            closeConnectionCount = 0;
        }



        dataGridView1.AutoGenerateColumns = false;

        dataGridView1.Columns.Add("filename", "File Name");
        dataGridView1.Columns.Add("openConnectionCount", "Open Connection");
        dataGridView1.Columns.Add("closeConnectionCount", "close Connection");

        dataGridView1.Columns[0].DataPropertyName = "filename";
        dataGridView1.Columns[1].DataPropertyName = "openConnectionCount";
        dataGridView1.Columns[2].DataPropertyName = "closeConnectionCount";

        var filenamesList = new BindingList<FileDetail>(fileDetails); // <-- BindingList
        dataGridView1.DataSource = filenamesList;


        //dataGridView1.DataSource = fileDetails;




        //dataGridView1.b

        //foreach (var fileDetail in fileDetails)
        //{
        //    label1.Text += "FileName:=" + fileDetail.filename + "\t Open Connection:= " +
        //                   fileDetail.openConnectionCount + "\t close connection:=" +
        //                   fileDetail.closeConnectionCount+"\n";
        //}
    }

Edit:-

I have followed some link for the same
How to bind list to dataGridView?
http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/766c7b46-6431-4ec1-8e74-50f718c6606b

  • 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-17T12:31:42+00:00Added an answer on June 17, 2026 at 12:31 pm

    Data binding only work with Set/Get Properties, not public field.

    Try to change your FileDetail like this :

    public class FileDetail
    {
        public  string filename { get; set; }
        public int openConnectionCount { get; set; }
        public int closeConnectionCount { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am quite new to JavaScript, I have written some code that just reloads
I have written an application inside a JFrame window, and would like to have
I have window form in my WPF Application when I open new window from
I have developed a windows application, which will read updated data from .jrn files(In
I have a command-line application written in C++ and built with gcc/make that runs
I have written a code that draws grids and a triangle inside one grid
I have written code to save an image which is generated by the application.
I have created a c# windows application and written 75% of the code. The
I have written some code that makes use of an open source library to
I have an windows service that monitors a folder for new files being created.when

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.