Click here to Skip to main content
14,770,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my save code:
string str = "";
                int row = dataGridView1.Rows.Count;
                int cell = dataGridView1.Rows[1].Cells.Count;
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < cell; j++)
                    {
                        if (dataGridView1.Rows[i].Cells[j].Value == null)
                        {
                            //return directly
                            //return;
                            //or set a value for the empty data
                            dataGridView1.Rows[i].Cells[j].Value = "";
                        }
                        str += dataGridView1.Rows[i].Cells[j].Value.ToString() + ",";
                    }
                }
                write.WriteLine(str);

                string str2 = "";
                int row2 = dataGridView2.Rows.Count;
                int cell2 = dataGridView2.Rows[1].Cells.Count;
                for (int i = 0; i < row2; i++)
                {
                    for (int j = 0; j < cell2; j++)
                    {
                        if (dataGridView2.Rows[i].Cells[j].Value == null)
                        {
                            //return directly
                            //return;
                            //or set a value for the empty data
                            dataGridView2.Rows[i].Cells[j].Value = "";
                        }
                        str2 += dataGridView2.Rows[i].Cells[j].Value.ToString() + ",";
                    }
                }
                write.WriteLine(str2);


What I have tried:

Here's the code I tried to use to open it but all it does it place the entire line into the first column.
foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        
                        cell.Value = read.ReadLine();
                    }
                }
Posted
Updated 1-Jun-17 12:43pm
Comments
j snooze 1-Jun-17 17:12pm
   
I'm not understanding. The title says you are trying to load a comma delimited text INTO a datagrid, but all I see is code trying to write a comma delimited line FROM the data in a datagrid. What am I missing?
j snooze 1-Jun-17 17:14pm
   
by the way I would also recommend making a method to pass in a datagridview and return a string of the output, basically you have the exact same code twice except you had to name all the variables with a "2" on them. When you see repeat code thats a good candidate for a single method.
Robmeister8911 1-Jun-17 17:45pm
   
The top one is a the small snippet of the save code that saves the datagridview rows into the text file.. The bottom box is the small snippet of me trying to load it.

Here's the entire streamreader code:
private void OpenToolStripMenuItem_Click_1(object sender, EventArgs e)
{
ofd.Filter = "WAP Files (*.wap)|*.wap";
ofd.Title = "Open File";

if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
textBox54.Text = read.ReadLine();


comboBox42.Text = read.ReadLine();

richTextBox23.Text = read.ReadLine();

checkBox18.Checked = Convert.ToBoolean(read.ReadLine());

foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Value = read.ReadLine();
}
}

foreach (DataGridViewRow row2 in dataGridView2.Rows)
{
foreach (DataGridViewCell cell2 in row2.Cells)
{
cell2.Value = read.ReadLine();
}
}


var loadedFileName = System.IO.Path.GetFileName(ofd.FileName);

label90.Text = loadedFileName;
fileName = loadedFileName;

read.Close();
read.Dispose();
}

}

1 solution

Instead of using ReadLine try treating the CSV file as a database and use ODBC or OleDb to read the file. There are a few articles here on CodeProject that can help you ...
Read Text File (txt, csv, log, tab, fixed length)[^]
Using OleDb to Import Text Files (tab, CSV, custom)[^]
(There are others).
Once you have the information in a dataset or datatable then you can use that as the DataSource to your DataGridView
   
Comments
Robmeister8911 1-Jun-17 17:44pm
   
I'm using ReadLine because I have so much more than a few datagridviews in my program. I'm loading over 50 textboxes, 18 checkboxes, 21 richtextboxes and 42 comboboxes. So the 2 datagridviews are minimal..

Will I still be able to read all of those by getting away from streamreader [unless I'm misunderstanding you]?
CHill60 1-Jun-17 18:19pm
   
I'm assuming you are saving the datagridviews into separate files? In which case you can still read them the way I have suggested. You might find this article more useful C# Class: Text file as DataBase[^] but if you are looking to save all of the contents of the controls then this article is probably better Saving the state (serializing) a Windows Form[^]
Robmeister8911 2-Jun-17 9:46am
   
No, the all of the data is being saved to the same file. This is so that when the user opens up the program they will open up a file that they need to edit and all of their data shows up in the boxes.
CHill60 2-Jun-17 17:34pm
   
In that case the last link I gave in my comment above would be appropriate

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900