datagridview passed through a constructor does not show columns or rows

Francisco Aravena García 21 Reputation points
2022-12-16T02:02:06.36+00:00

Hi,

I'm passing a datagridview through a constructor but not show info, although if I consult a specific cell if I see that it contains the information.
How show info in datagridview?, i try update() and refresh() method but not work.

    public partial class FrmSeleccionarProveedor : Form  
    {  
        public FrmSeleccionarProveedor()  
        {  
            InitializeComponent();  
        }  
  
        public FrmSeleccionarProveedor(DataGridView dataGridView)  
        {  
            InitializeComponent();  
            DgvProveedores = dataGridView;  
            Label1.Text = DgvProveedores.Rows[0].Cells[1].Value.ToString();  
        }  
  
    }  

271190-captura-de-pantalla-2022-12-15-225746.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,901 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,806 Reputation points
    2022-12-16T22:54:35.863+00:00

    You cannot "share" a UI control in multiple forms. A UI control is parented to the window that owns it. In your case that is your first form. Passing the DGV to another form and attempting to add it to that form isn't going to work. You should be passing the data between forms, not the UI controls.

    In the child form you are accessing the grid that is contained in the parent form. While not recommended, it will work. However in order to update the grid the parent form must be processing messages. If you are showing the child form modally (ShowDialog) then the parent form isn't responding to messages so don't expect the UI to change.

    The reason you're not seeing the DGV on the child form at all (even if it did work) is because you have to add a control to the Controls property of a form before it gets rendered. That is what the InitializeComponent method does. It creates an instance of the controls you've defined in the designer and then adds them to the Controls property of the form. In your constructor you then reassign the underlying field to a new control. That has no impact on the DGV that was added to the form and therefore the original (empty) DGV is getting shown in the form. Note that moving the assignment before the call to InitializeComponent wouldn't resolve this either because that method will create a new instance of the DGV anyway.

    As I already mentioned, you cannot share a UI control between forms. A control can only be rendered on 1 form. If you need the same data on multiple forms then pass the data between the forms. You can access a control on another form but this is considered to be bad design and not recommended. But that wouldn't help you here I believe.


2 additional answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,296 Reputation points Microsoft Vendor
    2022-12-16T09:00:40.11+00:00

    Hi @Francisco Aravena García , Welcome to Q&A,

    In fact, if your main form data is bound to it, it will display correctly.
    E.g:

    private void Form1_Load(object sender, EventArgs e)  
    {  
        DataTable dataTable = new DataTable();  
        dataTable.Columns.Add("Name", typeof(string));  
        dataTable.Columns.Add("Age", typeof(int));  
        dataTable.Columns.Add("Phone", typeof(string));  
        dataTable.Rows.Add("John", 30, "123");  
        dataTable.Rows.Add("Jane", 25, "234");  
        dataGridView1.DataSource = dataTable;  
    }  
      
    private void button1_Click(object sender, EventArgs e)  
    {  
        Form2 form2 = new Form2(dataGridView1);  
        form2.Show();  
    }  
    

    271258-image.png

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Francisco Aravena García 21 Reputation points
    2022-12-16T12:11:26.78+00:00

    Hi, thank you for reply,

    I try this but not work, what is wrong ?

    namespace WinFormsApp1  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                DataTable dataTable = new DataTable();  
                dataTable.Columns.Add("Name", typeof(string));  
                dataTable.Columns.Add("Age", typeof(int));  
                dataTable.Columns.Add("Phone", typeof(string));  
                dataTable.Rows.Add("John", 30, "123");  
                dataTable.Rows.Add("Jane", 25, "234");  
                dataGridView1.DataSource = dataTable;  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form2 form2 = new Form2(dataGridView1);  
                form2.Show();  
            }  
        }  
    }  
    
    namespace WinFormsApp1  
    {  
        public partial class Form2 : Form  
        {  
            public Form2()  
            {  
                InitializeComponent();  
            }  
      
            public Form2(DataGridView dataGridView)  
            {  
                InitializeComponent();  
                dataGridView1 = dataGridView;  
            }  
        }  
    }  
    

    271413-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.