简体   繁体   中英

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 :

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM