Loading cell values in dataGridView from text file C#

ChrisCreateBoss

I am using a StreamWriter to write all values on all exisiting cells in a dataGridView, that writer is working perfect. But my reader isn't doing its work very good.

This is my writer code(which has no problem):

 StreamWriter writer = new StreamWriter(valuePath);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                writer.WriteLine(cell.Value);
            }
        }
        writer.Close();

This is my reader code:

StreamReader sr = new StreamReader(valuePath);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                cell.Value = sr.ReadLine();
            }
        }
        sr.Close();

The output: Monday 1 Tuesday 2 Wednesday 3

This code is reading each line from the txt file into the "existing cells" in my dataGridView, so when the app is opened, there is only one row. That's why my reader is not working good. Can someone help me solve this??

chouaib

I suggest that you write your text file in this format:

cell1, cell2, ...

cell1, cell2, ...

cell1, cell2, ...

...

to do that, change your writer code to :

StreamWriter writer = new StreamWriter(valuePath);
string line;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    line = "";
    foreach (DataGridViewCell cell in row.Cells)
    {
        line += cell.Value.ToString();
    }
    writer.WriteLine(line);
}
writer.Close();

The reader code should be changed accordingly to:

StreamReader sr = new StreamReader(valuePath);
string line;
string[] values;
int n=0;

while(!sr.EndOfStream)
{
    line = sr.ReadLine();
    values = line.Split(',');

    for(int m=0; m<values.Length; m++)
    {
       dataGridView1[m, n].Value = values[m];
    }
    n++;
}
sr.Close();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Values for text box from a Excel cell in C#

From Dev

How can I extract semicolon separated values from DataGridView cell in C#

From Dev

trying to fill a Datagridview in C# with values from a JSON but i only get 1 cell fill

From Dev

How can I pass multiple cell values of datagridview from one form to another at same time in C#?

From Dev

C# retrieve cell value from datagridview

From Dev

comparison of datagridview cell values

From Dev

Multiline text in DataGridView Cell

From Dev

Loading a text file into a c program

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

Loading Text from a file into a Text box

From Dev

Retrieving Selected Row/Cell Values in DataGridView From one form to another

From Dev

How to get the text from current cell in datagridview textchanged event?

From Dev

VBA: Could not write cell values to text file

From Dev

DatagridView not loading from DataTable

From Dev

DatagridView not loading from DataTable

From Dev

Entity Framework, Multiple values 1 cell/ Datagridview c#

From Dev

How to pass c# datagridview selected cell values to SQL query

From Dev

how to add text to a dataGridView cell in different form c#

From Dev

Highlight the particular text inside a datagridview cell in c#windows application

From Dev

Change textcolor of a row in a datagridview if a cell contains part of text c#

From Dev

Loading values from properties file dynamically in GWT

From Dev

Loading values from properties file dynamically in GWT

From Dev

how to change cell values in datagridview according to a cell

From Dev

Merge DataGridView Image cell with text cell

From Dev

loading external text from .txt to a html file

From Dev

Loading data from a text file Informix

From Dev

loading an array from text file in javascript

From Dev

ActionScript: loading variables from a text file into _root

From Dev

Loading text from .txt file iOS

Related Related

  1. 1

    Get Values for text box from a Excel cell in C#

  2. 2

    How can I extract semicolon separated values from DataGridView cell in C#

  3. 3

    trying to fill a Datagridview in C# with values from a JSON but i only get 1 cell fill

  4. 4

    How can I pass multiple cell values of datagridview from one form to another at same time in C#?

  5. 5

    C# retrieve cell value from datagridview

  6. 6

    comparison of datagridview cell values

  7. 7

    Multiline text in DataGridView Cell

  8. 8

    Loading a text file into a c program

  9. 9

    from DataGridView saving to text file issue when saving date values on vb.net

  10. 10

    Loading Text from a file into a Text box

  11. 11

    Retrieving Selected Row/Cell Values in DataGridView From one form to another

  12. 12

    How to get the text from current cell in datagridview textchanged event?

  13. 13

    VBA: Could not write cell values to text file

  14. 14

    DatagridView not loading from DataTable

  15. 15

    DatagridView not loading from DataTable

  16. 16

    Entity Framework, Multiple values 1 cell/ Datagridview c#

  17. 17

    How to pass c# datagridview selected cell values to SQL query

  18. 18

    how to add text to a dataGridView cell in different form c#

  19. 19

    Highlight the particular text inside a datagridview cell in c#windows application

  20. 20

    Change textcolor of a row in a datagridview if a cell contains part of text c#

  21. 21

    Loading values from properties file dynamically in GWT

  22. 22

    Loading values from properties file dynamically in GWT

  23. 23

    how to change cell values in datagridview according to a cell

  24. 24

    Merge DataGridView Image cell with text cell

  25. 25

    loading external text from .txt to a html file

  26. 26

    Loading data from a text file Informix

  27. 27

    loading an array from text file in javascript

  28. 28

    ActionScript: loading variables from a text file into _root

  29. 29

    Loading text from .txt file iOS

HotTag

Archive