Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write the code to save the data from datagrid to the text file as follows
C#
private void Save_Click(object sender, EventArgs e)
{
    string filedata = string.Empty;
    string strcontent = string.Empty;
    string srEnd = string.Empty;
    if (dataGridView1.Rows.Count > 0)
    {
        m_flag = true;
        if (m_flag)
        {
            StreamReader sr = new StreamReader(Append.FileName);
            while (sr.Peek() >= 0)
            {
                srEnd = sr.ReadLine();
                if (srEnd.StartsWith("9"))
                {
                    strcontent = srEnd;
                }
            }
            sr.Close();
            StreamReader srNew = new StreamReader(Append.FileName);
            while (srNew.Peek() >= 0)
            {
                filedata = srNew.ReadToEnd();
                if (strcontent != string.Empty)
                    filedata = filedata.Replace(strcontent, "");
            }
            srNew.Close();
            StreamWriter swwrite = new StreamWriter(Append.FileName);
            swwrite.Write(filedata);
            swwrite.Close();
            m_flag = false;
            sr.Close();
        }

        if (m_flag == false)
        {
            List<string> lstContent = new List<string>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if ((string)row.Cells[0].Value == "FileControl")
                {

                    lstContent.Add((string)row.Cells[1].Value);

                    string mydata = string.Join(",", lstContent.ToArray());

                    using (StreamWriter sw = new StreamWriter(Append.FileName, true))
                    {
                        sw.WriteLine();
                        sw.Write(mydata);
                    }
                }
            }

        }
    }
}

My actual process is i will have a text file attached to a tree node. If the user fill the form and click on save i will save the text file as well as i will show the data in the datagrid. While writing to datagrid i will add a new line which was changed according to the entries by user.

Each and every length should be 94.

I am having 2 issues here

1) When i click save for multiple times a new line is created in my text file that i am writing my data.

2) If my data in the datagrid is as follows

101 111111111 1111111111010210452A094101
9000001000001000000010011111111000000000001000000000000
It works fine if i have some more data

101 111111111 1111111111010210452A094101
52001 1 1 CCD1 101021101021 1111111110000001 6201111111181 00000000011 1 1 0111111110000001 820000000100111111110000000000000000000000011 111111110000001 9000001000001000000010011111111000000000001000000000000
It is breaking some of the characters from the first line.

can any one help me please.
Posted
Updated 21-Oct-10 3:59am
v2

1 solution

I would say the sw.WriteLine(); is responsible for that. It's at the end of the method.

Good luck!
 
Share this answer
 
Comments
demouser743 22-Oct-10 3:54am    
But if i remove that i am unable to get the newly inserted data in the next line
E.F. Nijboer 22-Oct-10 12:23pm    
Why not change:
sw.WriteLine();
sw.Write(mydata);
into -> sw.WriteLine(mydata);

Also, users can probably add new rows with the DataGridView. If you look at the DataGridView at runtime you will see an empty line when you scroll down completely. Because you are working with the rows from a user interface control this empty line (intended for adding new rows) is also returned. So you should check the IsNewRow property and not add this row if this is true.
More info: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.isnewrow.aspx

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