Search code examples
c#datagridviewread-write

DatagridView displaying error when connected with filing


the GUI i am working with problem occurs while filing it is saving data amazingly and on retrieving the records on DataGridView here is the code i am trying but it is also showing the Last Value of column and 1st value of next column like this is the info in file and data from file is like: Why same data always??

3519,laiba,99

3519,maheen,89

here is my code:

 private void btnsave_Click(object sender, EventArgs e)
        {
            FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(f);
            sw.Write(txtregno.Text+","+txtname.Text+","+txtmarks.Text+";");
            MessageBox.Show("Saved Successfully");
            sw.Close();
        }

        private void btnshow_Click(object sender, EventArgs e)
        {
            FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(f);
            string data = sr.ReadToEnd();
            string[] lineWiseRecord = data.Split(';');
            foreach (string item in lineWiseRecord)
            {
                string[] colWiseRecord = item.Split(','); 
                    dataGridView1.Rows.Add(colWiseRecord[0], colWiseRecord[1], colWiseRecord[2] );
            }
            f.Close();
            sr.Close();
        }

Solution

  • Your code will throw an error and cannot be run. Specify the number of columns for the datagridview, and then load the data into the datagirdview through the foreach loop, so that you can see all the data in the txt document. Here is my code, you could try the following code to replace it.

    private void btnsave_Click(object sender, EventArgs e)
            {
                FileStream f = new FileStream("D://abc.txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(f);
                sw.Write(txtregno.Text + "," + txtname.Text + "," + txtmarks.Text + ";");
                MessageBox.Show("Saved Successfully");
                sw.Close();
            }
    
            
    
            private void btnshow_Click_1(object sender, EventArgs e)
            {
                FileStream f = new FileStream("D://abc.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(f);
                string data = sr.ReadToEnd();
                string[] lineWiseRecord = data.Split(';');
                dataGridView1.ColumnCount = 3;
                dataGridView1.Columns[0].Name = "regno";
                dataGridView1.Columns[1].Name = "Name";
                dataGridView1.Columns[2].Name = "Mark%";
    
                foreach (string item in lineWiseRecord)
                {
                    string[] colWiseRecord = item.Split(',');
                    dataGridView1.Rows.Add(colWiseRecord);
                }
                f.Close();
                sr.Close();
            }
    

    Result:

    enter image description here