namespace gridStudent
{
    public partial class Form2 : Form
    {
        public Form2(DataGridView dgvFromFom1)
        {

            foreach (DataGridViewColumn dc in dgvFromFom1.Columns)
            {
                dataGridView1.Columns.Add(dc.Name, dc.HeaderText);
            }

            foreach (DataGridViewRow dr in dgvFromFom1.Rows)
            {
                Object[] newRow = new object[dr.Cells.Count];

                for (int i = 0; i < newRow.Length; i++)
                {
                    newRow[i] = dr.Cells[i].Value;
                }
                dataGridView1.Rows.Add(newRow);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace gridStudent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Form2 f= new Form2(dataGridView1);
            f.Show();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(txtFirstName.Text, txtLastName.Text, txtMark1.Text, txtMark2.Text);

        }
    }

I am getting an exception saying object reference is not set to an instance....someone help

Edited 2 Years Ago by __avd: Wrong forum

obvious,
where is your dataGridView1 initialized in "Form2 f= new Form2(dataGridView1);"
Plus, if you want to fill the same details from one grid to another, why dont you use the same datasource, rather than having so much circus done inside.
The more code you write, the more bugs it leads to.

This article has been dead for over six months. Start a new discussion instead.