Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me.........
I have window1 and window 2, on window1 i have componen of DataGridView namely dgv, in window2 i use a code to call the name component of Datagridview from window1 but it didn't work, why ? and how the solution?

i use this code.

=============================
window1 frm = new window1()
frm.dgv.datasource = null;
=============================


when i create "frm." the autotext of "dgv" didn't appear...
Posted

1 solution

Create Parameterised constructor in form2 or window to which will use the datagridview of another window say window1 or form1 pass your datagridview as parameter from window1 or form1 as below


C#
//Code behind For Form1 or Window1
    public partial class Form1 : Form
    {
        public class AnEntity
        {
            public string name { get; set; }
            public int roll { get; set; }
        
        }
        public Form1()
        {
            InitializeComponent();
            List<anEntity> lstEntity = new List<anEntity>();
            AnEntity a = new AnEntity();
            a.name = "Anis";
            a.roll = 90;
            lstEntity.Add(a);
            a = new AnEntity();
            a.name = "Monojit";
            a.roll = 19;
            lstEntity.Add(a);
            a = new AnEntity();
            a.name = "Rubel";
            a.roll = 18;
            lstEntity.Add(a);
            dataGridView1.DataSource = lstEntity;

        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(dataGridView1);
            f.ShowDialog();
        }
    }

//Code behind For Form2 or Window2
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(DataGridView grdv)
        {
            InitializeComponent();

            label1.Text = "Total Row:" + grdv.Rows.Count.ToString();
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900