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 6338247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:24:24+00:00 2026-05-24T19:24:24+00:00

Possible Duplicate: Putting a .txt file into a DataGridView When I click an open

  • 0

Possible Duplicate:
Putting a .txt file into a DataGridView

When I click an open button I would like to choose a file and place it into a DataSource to futher be worked into a DataGridView.

Right now what I have looks like this:

OpenFileDialog openFile = new OpenFileDialog();

openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.RestoreDirectory = true;

try
{
    if (openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
    {
        // Right now I am loading the file into a RichTextBox
        openFileRTB.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);

        // What I would like to do is load it into a DataSource and then into a DataGridView.
        // So really I would like to remove the openFileRTB line of code and replace it.
        // That is where I need help :).
    }
}

catch (Exception)
{
    MessageBox.Show("There was not a specified file path to open.", "Path Not Found Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

Here is an example of a file I would be opening (space delimited):

Title1 Title2 Title3 Title4 Title5 Title6
abc123 abc123-123-123 225.123 123.456 180 thing99
c123 somethingHERE 987.123 123.456 360 anotherThing1
abc124 somethingHERE225.123 123.456 0 thing99

I am very unfamiliar with DataSource and DataGridView so if I could get some help with how it works and what needs to happen, how it would look, etc. that would be greatly appreciated. 🙂

Thanks.

  • 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-05-24T19:24:24+00:00Added an answer on May 24, 2026 at 7:24 pm

    You could split the lines and loop all rows/columns to generate the DataTable:

    For example(VB.NET):

    Dim separator = " "c
    Dim fileName = OpenFileDialog1.FileName
    Dim tbl As New DataTable(fileName)
    Dim query = From line In IO.File.ReadAllLines(fileName)
                Let row = line.Split(separator)
    
    If query.Any Then
       For Each col In query(0).row
             'build DataColumns from first line'
             tbl.Columns.Add(New DataColumn(col))
       Next
       If query.Count > 1 Then
           For rowIndex = 1 To query.Count - 1
               Dim newRow = tbl.NewRow
               For colIndex = 0 To query(rowIndex).row.Length - 1
                   Dim colValue = query(rowIndex).row(colIndex)
                   newRow(colIndex) = colValue
               Next
               tbl.Rows.Add(newRow)
           Next
       End If
    End If
    

    This works just as well without LINQ (now also in C# ;)):

    ....
    var rows = System.IO.File.ReadAllLines(fileName);
    if (rows.Length != 0) {
        foreach (string headerCol in rows(0).Split(separator)) {
            tbl.Columns.Add(new DataColumn(headerCol));
        }
        if (rows.Length > 1) {
            for (rowIndex = 1; rowIndex < rows.Length; rowIndex++) {
                var newRow = tbl.NewRow();
                var cols = rows(rowIndex).Split(separator);
                for (colIndex = 0; colIndex < cols.Length; colIndex++) {
                    newRow(colIndex) = cols(colIndex);
                }
                tbl.Rows.Add(newRow);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do you concatenate the rows of a matrix into a vector
Possible Duplicate: Parsing text in C Say I have written to a text file
Possible Duplicate: In Vim: How do I delete a word and go into insert
Possible Duplicate: In vim is there a way to delete without putting text in
Possible Duplicate: jquery - Is $(document).ready necessary? Putting the JS just above the </body>
Possible Duplicate: Putting a django login form on every page how can I add
Possible Duplicate: Ways around putting a password in code If you create an application
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page

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.