我有一个显示DataGridView的简单表单;我想在同一表格的第一个下面显示第二个DataGridView对象。
但是当我运行代码时,它只显示第一个代码。在运行调试器时,表单会列出它同时链接了两个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();
表格:
答案 0 :(得分:1)
首先:使用System.Windows.Forms.DockStyle.Fill;
,您无法看到两个dataGrid都已创建,
所以测试你的两个dataGridView没有这一行。
或者您可以使用第一个:dg.Dock = System.Windows.Forms.DockStyle.Top;
和第二个:dg.Dock = System.Windows.Forms.DockStyle.Bottom;
此外,您可以使用form.Controls.Add(dg);
代替dg.Parent = form;