简体   繁体   中英

c# Winforms: Data skipped one row when exporting to datagridview in different form

My code below caused the data exported from richtextbox in Form 1 to skipped one row when displayed in datagridview in Form 2. I would like it to display at the first row. 多余的输出

Below are my codes.

Form 1

namespace Delivery
{
    public partial class Form1 : Form
    {
        public static string passingtext;
        public static string passingtext1;
        public static string passingtext2;
        public static string passingtext3;
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            FormsCollection.Form2.Hide();
            passingtext = RichTextBox1.Text;
            passingtext1 = RichTextBox2.Text;
            passingtext2 = RichTextBox3.Text;
            passingtext3 = RichTextBox4.Text;
            Form2 dg = new Form2();
            FormsCollection.Form2.Show();
        }
    }
}

Form 2

namespace Delivery
{
    public partial class Form2: Form
    {
        public static string passingtext;
        public static string passingtext1;
        public static string passingtext2;
        public static string passingtext3;
        public Form2()
        {
            InitializeComponent();
        }
        private void datagrid_Load(object sender, EventArgs e)
        {
            DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
            row.Cells[0].Value = Todatagrid.passingtext;
            row.Cells[1].Value = Todatagrid.passingtext1;
            row.Cells[2].Value = Todatagrid.passingtext2;
            row.Cells[3].Value = Todatagrid.passingtext3;

            dataGridView1.Rows.Add(row);
        }
    }
}

Hope to get some help thanks.

Use

   dataGridView1.Rows[0] = row;

or

  this.dataGridView1.Rows.Insert(0, row);      

instead of

   dataGridView1.Rows.Add(row);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM