Jump to content

Need to read data from text file and put it into a DataGrid

- - - - -

  • Please log in to reply
15 replies to this topic

#1
biscuits

biscuits

    Newbie

  • Members
  • Pip
  • 8 posts
I am making a windows form application and I need to display a list of order details on a form. (the orders to be shown are from more than one person and each order has 8 fields so i'll need 8 columns) I've managed to use StreamReader() to read 8 lines of text from a file into 8 textboxes, but this only shows one order, so instead of having millions of textboxes, i'd like a datagrid to show this information. The text file i'm using has each piece of information on a different line, looking like this:

Customer Name
Customer Nick Name
Customer Address
Order date
Order time
Type of meal
Special delivery requirements
Order status
Customer Name
Customer Nick Name
Customer Address
Order date
Order time
Type of meal
Special delivery requirements
Order status
Customer Name
Customer Nick Name
Customer Address
Order date
Order time
Type of meal
Special delivery requirements
Order status

Is it possible to use StreamReader() to read this information and put it straight into columns on my datagrid when a button is clicked?

Any help is appreciated, Many thanks.

Alexander.

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
I am sure you must have seen my tutorial on "Advanced Serialization" , you could serialize a Object into a file and then Type cast the Object stored in file to "Retrieve the Details"

#3
biscuits

biscuits

    Newbie

  • Members
  • Pip
  • 8 posts
Problem is that this is for an assignment and I NEED to use streamreader and I have to use the text file provided and I cant edit the text file at all, so I need a good way of displaying the data from the text file on a form so you can see all of the orders (including ones I have added to it using streamwriter). Nice guide though thanks for the input

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Would this work?

StreamReader sr= File.OpenText("file.txt");
string input = null;
List<string> stringList = new List<string>();

while ((input = sr.ReadLine()) != null){
  stringList.add(input);
}
And then set this list as datasource for the datagrid? (might need to databind it after setting the datasource)
+ don't forget to close the streamreader.

#5
biscuits

biscuits

    Newbie

  • Members
  • Pip
  • 8 posts
What's the code to set the list as the datasource for the datagrid?

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
datagridName.datasource = list;
datagridName.DataBind();

.. i think ..

#7
Sophex

Sophex

    Newbie

  • Members
  • Pip
  • 4 posts
if you manneged to read the data to the app gitting it to datagridview isnt a problem

you can add each columns with
dataGridView1.Columns.Add("colName", "ColTitle");
and then just make an array with all the data like string [] str
and the just add it ti the dataGridView with dataGridView1.Rows.Add(str);
and it will add your line
and then just do a lop

you can add a row that is smaller from the number of culomns bt you do the oppsite

hope I helped you in some way :D

#8
biscuits

biscuits

    Newbie

  • Members
  • Pip
  • 8 posts
this is my code so far:


namespace ReaderTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            TextReader reader = new StreamReader(@"C:\Users\Alex\Documents\Visual Studio 2010\Projects\ReaderTest\orders.txt");

            string contents = reader.ReadToEnd();

            string[] myArray = {contents};

            foreach (string gotya in myArray)

            {


                dataGridView1.Columns.Add("colname", "Name");

                dataGridView1.Rows.Add(contents);

                dataGridView1.DataSource = gotya; 

            }


            reader.Close();

        }


        

    }

}


Still cant figure out how to get it into the datagrid, when I use your commands to add columns and rows I get errors - one of them being this "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

I can get all the writing fot the .txt file to go into a textbox on the right lines etc but it's not very nice to display this type of information in a textbox

#9
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
You need to First Databind with a datasource and only then "Add a new DataRow"

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If your datagrid is empty from the start, try:
dataGridView1.Columns.Add("colname", "Name");

int i=0;
foreach( string gotya in myArray )
{
  dataGridView1.Rows.Add();
  dataGridView1.Rows[i].Cells[0]).Value = gotya;
  i++;
}


#11
biscuits

biscuits

    Newbie

  • Members
  • Pip
  • 8 posts

oxano said:

If your datagrid is empty from the start, try:
dataGridView1.Columns.Add("colname", "Name");

int i=0;
foreach( string gotya in myArray )
{
  dataGridView1.Rows.Add();
  dataGridView1.Rows[i].Cells[0]).Value = gotya;
  i++;
}

This is working, the text from the .txt file is going into the datagrid but its all going as one string into one cell, I need 8 different columns with unlimited rows. is there a way to split the string whenever there is a blank space in the file? I'm only a beginner at c# but I remember seeing a split() method somewhere. Thanks very much for the input though people.

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Yes just string.Split(' ');
Very simple, you'll just need an extra loop + counter to loop trough the splitted string + to determine the column to place it in.
int i=0;
foreach( string gotya in myArray )
{
  string[] values = gotya.Split(' ');

  int j=0;
  foreach( string value in values)
  {
    dataGridView1.Rows.Add();
    dataGridView1.Rows[i].Cells[j]).Value = value;
    j++
  }
  i++;
}

I actually prefer normal for loops for(int i=0 ; i<myArray.Length ; i++) when i use a counter but it'll work.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users