![](/img/trans.png)
[英]Exporting Data From Multiple DataGridViews on a Form to Multiple Excel Sheets in a Single Excel File
[英]Multiple DataGridViews on 1 Form
我有一个显示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();
形式:
首先:使用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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.