How to add row in datagridview from another form?

12,045

put this on Form1

  private void button1_Click(object sender, EventArgs e) {
        Form2 f2 = new Form2(this);
        f2.Show();
  }

put this on Form2

    private Form1 form1;

    public Form2(Form1 form1) {
        InitializeComponent();  
        this.form1 = form1;
    }

    private void button1_Click(object sender, EventArgs e) {
        form1.dataGridView1.Columns.Add("FirstName", "First Name");
        form1.dataGridView1.Columns.Add("LastName", "Last Name");
        form1.dataGridView1.Columns.Add("UserId", "Userid");
        form1.dataGridView1.Columns.Add("Success", "Success");

        object[] row = new object[] {"1","Product 1","1000",DateTime.Now.ToString()};

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

that should do it

and datagridview Modifier should be public

Share:
12,045
vijisha
Author by

vijisha

Updated on June 04, 2022

Comments

  • vijisha
    vijisha almost 2 years

    i have 2 forms. form1 contains the datagridview. the second(form2) form contains textboxs. when i click Ok button in form2, the values should get added in datagridview as new row. This is the code i am trying to use to pass the data,but its neither showing error nor result.

    Form2

    private void btnOk_Click(object sender, EventArgs e)
    { 
      form1.datagridview.Rows.Add("firstname", "lastname", "Success", "Userid", DateTime.Now.ToString());
    }