I write the code to save the data from datagrid to the text file as follows
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.