Returning a selected record from a text file to Data Grid View control

John 466 Reputation points
2023-08-14T11:40:21.8966667+00:00

I may have noticed that when reading text files and importing them to a Data Grid View control, they only read character by character if I only show the first record for example.

How would I need to concatenate a record that's already been filtered?

In other words, how would I need to just display a record that is displayed all in one line, for example:

In the names text file:

John

Its output:

J

o

h

n

In a single record in a data grid view control, it supposed to be like this:

John

If there are other ways to code this, that would be great.

Also, there should be some tutorials on how to read and write text line by line, not character by character.

Here is the code snippet, but it needs to be improved:

public class Example        {            public string FirstName { get; set; }                                } private void button1_Click(object sender, EventArgs e) {   var results = File.ReadAllLines("C:\\Users\\johnc\\OneDrive\\Documents\\Names.txt");           var result = (from x in results.First()                  select new Example                 {                     FirstName = x.ToString();                 }).ToList();               dataGridView1.DataSource = result; }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,927 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,409 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,561 Reputation points
    2023-08-14T17:13:48.88+00:00

    Here is a working example, drop a DataGridView on a form.

    Create a text file, name it names.txt with the following.

    Karen
    Bob
    John
    Mary
    

    Set copy to output folder to copy if newer.

    Form code

    namespace WinFormsApp1;
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            dataGridView1.DataSource = 
                File.ReadAllLines("Names.txt")
                    .Select(x => new Item()
                    {
                        FirstName = x
                    })
                    .ToList();
        }
    }
    
    public class Item
    {
        public required string FirstName { get; set; }
    }
    

    Screenshot

    A1

    If you want to order the names

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            dataGridView1.DataSource = 
                File.ReadAllLines("Names.txt")
                    .Select(x => new Item()
                    {
                        FirstName = x
                    }).OrderBy(x => x.FirstName)
                    .ToList();
        }
    }
    

    Also, if there are empty lines the following skips them.

    dataGridView1.DataSource = 
        File.ReadAllLines("Names.txt")
            .Where(line => !string.IsNullOrWhiteSpace(line))
            .Select(line => new Item()
            {
                FirstName = line
            }).OrderBy(item => item.FirstName)
            .ToList();
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. mka mryn 0 Reputation points
    2023-08-14T16:57:47.01+00:00

    It appears that you've noticed a character-by-character reading issue when importing text files into a Data Grid View control, especially when displaying the first record. To concatenate a filtered record for a single-line display, you should modify your code to read and consolidate complete lines from the text file instead of reading characters individually. This will ensure that the record is shown correctly in a single line within the Data Grid View control.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.