Ryan
Ryan

Reputation: 29

Multiple DataGridViews on 1 Form

I have a simple form that displays a DataGridView; and I would like to display a second DataGridView object below the first on the same form.

But when i run the code, it only displays the first. When running through the debugger, the form lists that it has both datagridviews linked to it.

System.Windows.Forms.Form form
          = new System.Windows.Forms.Form();
        form.Size = new System.Drawing.Size(450, 400);
        form.Text = "Form";

        DataGridView dg = new DataGridView();
        dg.AllowUserToAddRows = false;
        dg.AllowUserToDeleteRows = false;
        dg.AllowUserToOrderColumns = true;
        dg.Dock = System.Windows.Forms.DockStyle.Fill;
        dg.Location = new System.Drawing.Point(0, 0);
        dg.ReadOnly = true;
        dg.TabIndex = 0;
        dg.DataSource = dt;
        dg.Parent = form;



        DataGridView dgHangers = new DataGridView();
        dgHangers.AllowUserToAddRows = false;
        dgHangers.AllowUserToDeleteRows = false;
        dgHangers.AllowUserToOrderColumns = true;
        dgHangers.Dock = System.Windows.Forms.DockStyle.Fill;
// attempting get the bottom of the first DataGridView() so the second will display below.
        dgHangers.Location = new System.Drawing.Point(0, dg.DisplayRectangle.Bottom);
        dgHangers.ReadOnly = true;
        dgHangers.TabIndex = 1;
        dgHangers.DataSource = hangerTable;
        dgHangers.Parent = form;



        form.ShowDialog();

Form :

Upvotes: 0

Views: 1449

Answers (1)

Beldi Anouar
Beldi Anouar

Reputation: 2180

First : with System.Windows.Forms.DockStyle.Fill; you can not see both dataGrid created , So test without this line for your two dataGridView.

Or you can use for the first : dg.Dock = System.Windows.Forms.DockStyle.Top;

and for the second : dg.Dock = System.Windows.Forms.DockStyle.Bottom;

alsoo you can use form.Controls.Add(dg); instead of dg.Parent = form;

Upvotes: 1

Related Questions