我有两个Form1,它有DataGridView和Form2,通过它插入数据但是在成功插入DataGridView后没有反映插入的新数据,我必须关闭Form1然后重新打开才能查看它 .

我是c#的新手,并获得了将DataGridView导出到网站的文本文件的代码(我不记得哪个...)并实现了它 .

这是我从Form2插入数据的代码:

void writetotxt()
    {

        string appfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyappData");
        string clientsdatadir = Path.Combine(appfolder, "Clients");
        string clientslogfilepath = Path.Combine(clientsdatadir, "Clientsprofiles.txt");
        System.IO.StreamWriter fileclients = new System.IO.StreamWriter(clientslogfilepath, true);
        try
        {
            string sLine = "";

            for (int r = 0; r <= dataGridView2.Rows.Count - 1; r++)
            {
                for (int c = 0; c <= dataGridView2.Columns.Count - 1; c++)
                {
                    sLine = sLine + dataGridView2.Rows[r].Cells[c].Value;
                    if (c != dataGridView2.Columns.Count - 1)
                    {
                        sLine = sLine + ";";
                    }
                }
                fileclients.WriteLine(sLine);
                sLine = "";

            }
            fileclients.Close();
            System.Windows.Forms.MessageBox.Show("Saved Succesfully","info", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            fileclients.Close();
        }
    }


    void createid()
    {
        dataGridView2.Rows.Add(textboxnamecreate.Text, maskedTextBoxcntctm.Text, maskedTextBoxlndlines.Text, textBoxemailcreate.Text, richTextBox1addressofc.Text, textBox2xdata.Text, richTextBox2remarkscreate.Text);            
        writetotxt();
        ClearAllFieldsc(this);

    }      

    private void createclientidbtn_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textboxnamecreate.Text))
        {

            createid();
            pictureBox2.Visible = true;
            timer1.Start(); 


        }
        else
            MessageBox.Show("Cannot Leave Name Blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

这是我在Form1上的代码:

public void loadclientdetails()
    {
        String sLine = "";
        try
        {
            string appfolder =  Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SolicitorData");
            string clientsdatadir = Path.Combine(appfolder, "Clients");
            string clientslist = Path.Combine(clientsdatadir, "Clientsprofiles.txt");
            System.IO.StreamReader FileStream = new System.IO.StreamReader(clientslist, true);
            dataGridView3.AllowUserToAddRows = false;
            sLine = FileStream.ReadLine();
            string[] s = sLine.Split(';');
            sLine = FileStream.ReadLine();
            while (sLine != null)
            {
                dataGridView3.Rows.Add();
                for (int i = 0; i <= s.Length - 1; i++)
                {
                    //Splits each line in the text file into a string array
                    s = sLine.Split(';');
                    //Sets the value of the cell to the value of the text retreived from the text file.
                    dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[i].Value = s[i].ToString();
                }
                sLine = FileStream.ReadLine();
            }
            //Close the selected text file.
            FileStream.Close();
        }
        catch
        {
        }
        int total;
        total = dataGridView3.Rows.Count / 2;
        textBoxtotalclients.Text = total.ToString();
    }

    private void ClientsProfile_Load(object sender, EventArgs e)
    {
        loadclientdetails();                           
    }

我搜索了答案,发现了这个:

C# refresh DataGridView when updating or inserted on another form

我试过apomene提供的答案:

在Form1上

public void loadclientdetails()
 {
    //code
 }

然后在Form2上定义:

Form1 obj = (Form1)Application.OpenForms["Form1"];

private void button1_Click(object sender, EventArgs e)
{
    obj.loadclientdetails();
    datagridview1.Update();
    datagridview1.Refresh();
 }

但现在DataGridView中的所有数据都会重复显示,我必须再次关闭Form并重新打开以正确查看 .

谢谢你的时间和帮助