Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:54:27+00:00 2026-05-17T01:54:27+00:00

Hi all i write a code to write my last row of datagrid view

  • 0

Hi all i write a code to write my last row of datagrid view to a file as follows

    private void Save_Click(object sender, EventArgs e)
    {
        if (dataGridView1.Rows.Count > 0)
        {
            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);
                    }
                }
            }

        }


    }

But if i click multiple times on save this is writing that line multiple times what i need is if already that line exists in the file i have to replace that line with new line. Any help please

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:54:28+00:00Added an answer on May 17, 2026 at 1:54 am

    Your StreamWriter is explicitly using the file with append = true. Change the second parameter of the constructor to false if you want to overwrite the file each time. Docs are here. Quote:

    append

    Type: System.Boolean

    Determines
    whether data is to be appended to the
    file. If the file exists and append is
    false, the file is overwritten. If the
    file exists and append is true, the
    data is appended to the file.
    Otherwise, a new file is created.

    Revised code:

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

    Replacing a given line in your file rather than just overwriting the whole file is a lot more difficult – this code is not going to get it done. StreamWriter is not great for this, you need random access and the ability to replace one data segment (line) by a different data segment of different length, which is an expensive operation on disk.

    You might want to keep the files in memory as a container of Strings and do your required line replacement within the container, then write out the file to disk using File.WriteAllLines – that’s if the file is not too big.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We all write reusable classes and code. We factor in configurability to allow us
I've writen an Excel-based, database reporting tool. Currentely, all the VBA code is associated
Now is time to write all for web-based applications? Write web applications is slower
How does TCP/IP report errors when packet delivery fails permanently? All Socket.write() APIs I've
I write my app in VS 2008 and so use all the fanciful stuffs
Before I write my own I will ask all y'all. I'm looking for a
I am trying to write a regular expression to strip all HTML with the
my goal is to write a stored proc that can collect all field values
I am trying to write a replacement regular expression to surround all words in
I try to write a simple client/server application (all application is a bluetooth service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.