Owais Khan
Owais Khan

Reputation: 1

Overwriting and Saving File using I/O and Datagridview

Iam using datagridview with two buttons (one for saving new file each time and other for loading the existing file) on run time after typing data to datagridview and pressing the save button a new file is created(which is alright) but when i press load button and select a file and after editing it and pressing save button it's creating a new file again. but i want that file to be overwritten (with newly added data to that old file which was previously saved). plz correct my code.

namespace DGVIEW_FILING
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {

                string fn = string.Format(@" {0}.txt", DateTime.Now.Ticks);
                FileStream FS = new FileStream(fn, FileMode.Create);




                // write the text from streamwriter.....
                // write the text from streamwriter.....
                TextWriter view = new StreamWriter(FS);



                int rc = dataGridView1.Rows.Count;

                for (int i = 0; i < rc - 1; i++)
                {
                    view.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "\t" +
                    dataGridView1.Rows[i].Cells[1].Value.ToString() + "\t" +
                  dataGridView1.Rows[i].Cells[2].Value.ToString());
                }



                view.Close();   // for close the fs file which created earlier......

            MessageBox.Show("DATA SAVED!! ");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            DialogResult obj = new DialogResult();
            //obj = openFileDialog1.ShowDialog();
            obj= DialogResult.OK;

            // Read the text from text file....
            if ( openFileDialog1.ShowDialog() == obj ) 
            {




                TextReader done = new StreamReader(openFileDialog1.FileName );
               // FileStream fileStream = new FileStream(@" {0}.txt", FileMode.Open, FileAccess.Write);
                int row = 0;
                string line;
                while ((line = done.ReadLine()) != null)
                {
                    string[] columns = line.Split('\t');
                    dataGridView1.Rows.Add();
                    for (int i = 0; i < columns.Length; i++)
                    {
                        dataGridView1[i, row].Value = columns[i];
                    }
                    row++;


                }

                MessageBox.Show(done.ReadToEnd()); 

                done.Close();
            }




            }


        }
    }

Upvotes: 0

Views: 789

Answers (1)

ari k
ari k

Reputation: 179

On load, store the filename. On save, if a filename has been stored, use that one instead (and then reset it to null).

string currentFile = null;
...
currentFile = openFileDialog1.FileName;
...
if (currentFile != null) {
    fn = currentFile;
    currentFile = null;
}

Upvotes: 0

Related Questions