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

Recommended Answers

All 5 Replies

i wanna transfer data from one form to another form using datagrid view

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.

i am sorry....but i dunno hw to intialise datagridview1

i am sorry....but i dunno hw to intialise datagridview1

Did you made datagridview1 through the designer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.