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

  • SEARCH
  • Home
  • 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 9160283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:42:07+00:00 2026-06-17T13:42:07+00:00

hi guys i am new to c# and i was making a text editor

  • 0

hi guys i am new to c# and i was making a text editor program using data grid view. The problem is when i open a text file to my data grid, then when i press save i get the error saying “Index was outside the bounds of the array”. I included my code below. Any suggestions can be a big help for me. I included my code below.

         // edit file button
                dataGridView1.Columns.Add("colname", "Question");
                dataGridView1.Columns.Add("colname", "Answer 1");
                dataGridView1.Columns.Add("colname", "Answer 2");
                dataGridView1.Columns.Add("colname", "Answer 3");
                dataGridView1.Columns.Add("colname", "Answer 4");
                dataGridView1.Columns.Add("colname", "Correct Answer");
                dataGridView1.Columns[5].ReadOnly = true;
                dataGridView1.Columns[5].DefaultCellStyle.BackColor = Color.DimGray;
                dataGridView1.AllowUserToAddRows = true;


                StreamReader sr = new StreamReader(fName);
                string Contents = sr.ReadToEnd();
                string[] strArray = { Contents };

                for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
                {
                    for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
                    {

                        dataGridView1.Rows.Add(strArray[r].Split('|')); //getting the error here
                    }

                }

      //save button
    private void button2_Click(object sender, EventArgs e)
    {
        //dataGridView1.AllowUserToAddRows = false;
        if (dataGridView1.Visible)
        {

            string fName = "C:\\Documents and Settings\\" + txtFileName.Text + ".txt";                
            System.IO.StreamWriter file = new System.IO.StreamWriter(fName);
            string sLine = "";

            for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
            {
                for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
                {
                    sLine = sLine + dataGridView1.Rows[r].Cells[c].Value;
                    if (c != dataGridView1.Columns.Count - 1)
                    {
                        sLine = sLine + "|";
                    }
                }
                file.WriteLine(sLine);
                sLine = "";
            }
            file.Close();
            System.Windows.Forms.MessageBox.Show("Save Complete.", "Program Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dataGridView1.DataSource = "";
            dataGridView1.Visible = false;
        }
        else
        {
            string cap = "Confirmation";
            string message = "Nothing to save";
            MessageBoxButtons Btn2 = MessageBoxButtons.OK;    
            MessageBox.Show(message, cap, Btn2);
        }            
    } 

Thanks again for the help

  • 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-06-17T13:42:08+00:00Added an answer on June 17, 2026 at 1:42 pm

    Initially there is a dataGridView1.Rows.Count

    Inside the loop u r doing this
    dataGridView1.Rows.Add, which increments dataGridView1.Rows.Count by 1 every single time

    so it comes back into the loop after the processing

    try declaring int gridRowcount = dataGridView1.Rows.Count;
    
    and then replace (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
    for (int r = 0; r <= gridRowcount - 1; r++) and try
    

    just guessing

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

Sidebar

Related Questions

So I'm having a problem making a new socket. I had this program that
Hi Guys new to this site but a big fan. Right the problem. It's
Guys i am very new to jQuery. I have started using the auto complete
Hey guys I'm making a chat program and I have a parent MessagePacket class
Hi guys, I am making some iPhone graph thingy. Here is my problem. I
Hey guys, I'm new to Objective-C, and I'm making an app, and when you
Hey guys I am new to android development, I am currently making an application.The
Hi guys im new to php and im making a submission form where people
Ok guys im new to jquery and have been making considerable progress but this
Hey Guys I Am really new to making apps and this a error that

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.