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.
Need to read data from text file and put it into a DataGrid
Started by biscuits, Jul 22 2010 11:54 AM
15 replies to this topic
#1
Posted 22 July 2010 - 11:54 AM
|
|
|
#2
Posted 23 July 2010 - 02:57 AM
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
Posted 23 July 2010 - 03:21 AM
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
Posted 23 July 2010 - 05:54 AM
Would this work?
+ don't forget to close the streamreader.
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
Posted 23 July 2010 - 06:35 AM
What's the code to set the list as the datasource for the datagrid?
#6
Posted 24 July 2010 - 08:27 AM
datagridName.datasource = list;
datagridName.DataBind();
.. i think ..
datagridName.DataBind();
.. i think ..
#7
Posted 25 July 2010 - 06:28 AM
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
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
Posted 26 July 2010 - 08:04 AM
this is my code so far:
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
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
Posted 26 July 2010 - 10:26 PM
You need to First Databind with a datasource and only then "Add a new DataRow"
#10
Posted 26 July 2010 - 11:11 PM
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
Posted 27 July 2010 - 04:04 AM
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
Posted 27 July 2010 - 04:15 AM
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.
I actually prefer normal for loops for(int i=0 ; i<myArray.Length ; i++) when i use a counter but it'll work.
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


Sign In
Create Account

Back to top









