简体   繁体   English

1个表单上的多个DataGridViews

[英]Multiple DataGridViews on 1 Form

I have a simple form that displays a DataGridView; 我有一个显示DataGridView的简单表单; and I would like to display a second DataGridView object below the first on the same form. 并且我想在同一表格的第一个下方显示第二个DataGridView对象。

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. 通过调试器运行时,该表单列出它具有链接到其的两个datagridviews。

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; 首先:使用System.Windows.Forms.DockStyle.Fill; you can not see both dataGrid created , So test without this line for your two dataGridView. 您看不到两个dataGrid都创建了,因此请为您的两个dataGridView没有这一行进行测试。

Or you can use for the first : dg.Dock = System.Windows.Forms.DockStyle.Top; 或者您可以使用第一个: dg.Dock = System.Windows.Forms.DockStyle.Top;

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

alsoo you can use form.Controls.Add(dg); 你也可以使用form.Controls.Add(dg); instead of dg.Parent = form; 而不是dg.Parent = form;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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